com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `x`
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `x`
·
记录一下有点蠢的问题,序列化再次序列化就会出现这种问题。
[2022-05-16 10:18:18.645] [ERROR] com.fu.common.global.GlobalExceptionHandler : 46 - 500异常:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.fu.common.entity.TokenInfo` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"token":"89d5aa16-5b58-4d1d-ac34-68470183697d","userId":1,"userName":"admin","userDept":[{"deptId":1,"parentDeptId":0,"deptName":"全部部门","createTime":1652586793000},{"deptId":2,"parentDeptId":1,"deptName":"研发部门","createTime":1652599366000}],"userRole":[{"roleId":2,"roleName":"java开发工程师","disabled":false,"createTime":1652599420000},{"roleId":1,"roleName":"超级管理员","disabled":false,"createTime":1652586829000}],"userRoleMenu":[{"menuId":1,"parentMenuId":0,"menuName":"全部菜单","menuPower":"insert;delete;update;select","createTime":1652586965000},{"menuId":2,"parentMenuId":1,"menuName":"用户中心","menuPower":"","createTime":1652599386000}]}')
at [Source: (String)""{\"token\":\"89d5aa16-5b58-4d1d-ac34-68470183697d\",\"userId\":1,\"userName\":\"admin\",\"userDept\":[{\"deptId\":1,\"parentDeptId\":0,\"deptName\":\"全部部门\",\"createTime\":1652586793000},{\"deptId\":2,\"parentDeptId\":1,\"deptName\":\"研发部门\",\"createTime\":1652599366000}],\"userRole\":[{\"roleId\":2,\"roleName\":\"java开发工程师\",\"disabled\":false,\"createTime\":1652599420000},{\"roleId\":1,\"roleName\":\"超级管理员\",\"disabled\":false,\"createTime\":1652586829000}],\"userRoleMenu\":[{\"menuId\":1,\"p"[truncated 220 chars]; line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1455)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1081)
at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:371)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:323)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1408)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:176)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:166)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4526)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3468)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3436)
at com.fu.base.controller.LoginController.login(LoginController.java:113)
at com.fu.base.controller.LoginController$$FastClassBySpringCGLIB$$b680891e.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
at com.fu.common.aop.LogAspect.doAround(LogAspect.java:86)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692)
at com.fu.base.controller.LoginController$$EnhancerBySpringCGLIB$$32a0d247.login(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
出现上面这种问题就是已经序列化还再次序列化。如下这种错误写法:
//存到redis里面的时候已经序列化过了,取出来的时候,new ObjectMapper().writeValueAsString(String,Object.class);这里的String是要序列化以后的字符串。但是我对它进行了2次序列化,所以会报上面的错。
redisTemplate.opsForValue().set(token,new ObjectMapper().writeValueAsString(tokenInfo),tokenOvertime, TimeUnit.SECONDS);
System.out.println(new ObjectMapper().readValue(new ObjectMapper().writeValueAsString(redisTemplate.opsForValue().get(token)),TokenInfo.class));
正确的做法是直接对其.toString();即可。
//对new ObjectMapper().readValue(params,Object.class);对里面的params.toString()即可。因为上一步存放到redis的时候已经对对象进行序列化了。
redisTemplate.opsForValue().set(token,new ObjectMapper().writeValueAsString(tokenInfo),tokenOvertime, TimeUnit.SECONDS);
System.out.println(new ObjectMapper().readValue(Objects.requireNonNull(redisTemplate.opsForValue().get(token)).toString(),TokenInfo.class));
jackson对象转Map
public static final ObjectMapper jsonMapper = new ObjectMapper();
jsonMapper.convertValue(data6201, Map.class));
jackson JSON字符串转对象
多层泛型要用TypeReference,没有泛型可以直接转对象
//GzybResponse2<T>
public static final ObjectMapper jsonMapper = new ObjectMapper();
jsonMapper.readValue(res, Object.class);//没有泛型直接转成相应的对象,如:GzybResponse2.calss
jsonMapper.readValue(res, new TypeReference<GzybResponse2<Data6201Res>>() {});//GzybResponse2对象包含泛型对象,就要用TypeReference
更多推荐
已为社区贡献12条内容
所有评论(0)