开发者

SpringMVC框架使用jackson封装数据过程中遇到的问题及解决

目录
  • SpringMVC使用jackson封装数据过程中遇到问题
    • 抛出异常日志
  • SpringMVC使用jackson解析数据报错
    • 总结

      SpringMVC使用jackson封装数据过程中遇到问题

      首先这是在springmvc框架下使用jackson封装数据遇到的问题。

      如果你遇到的异常跟我下面日志里的一样,请继续往下看。

      这是一个jackson版本引起的问题,到maven仓库https://mvnrepository.com/search?q=jackson搜索jackson最新的版本就能解决问题了,亲测有效。

      抛出异常日志

      14-Jul-2020 09:54:11.594 警告 [RMI TCP Connection(19)-127.0.0.1] org.springframework.context.support.AbstractApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Constructor threw exception; nested exception is Java.lang.NoClassDefFoundError: com/fasterXML/jackson/databind/exc/InvalidDefinitionException

      14-Jul-2020 09:54:11.597 严重 [RMI TCP Connection(19)-127.0.0.1] org.springframework.web.servlet.FrameworkServlet.initServletBean Context initialization failed

       org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException

          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1320)

          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1214)

          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)

          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)

          at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)

          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)

          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)

          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)

          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:893)

          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)

          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)

          at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702)

          at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668)

          at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716)

          at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591)

          at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:530)

          at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170)

          at javax.servlet.GenericServlet.init(GenericServlet.java:158)

          at org.apache.catalina.core.Standardwrapper.initServlet(StandardWrapper.java:1144)

          at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091)

          at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:983)

          at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4978)

          at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5290)

          at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

          at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)

          at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)

          at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)

          at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1736)

          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.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)

          at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)

          at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)

          at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482)

          at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431)

          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.apache.tomcat.util.modeler.BaseModelMBandroidean.invoke(BaseModelMBean.java:300)

          at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)

          at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)

         &nbjssp;at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)

          at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)

          at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)

         &n编程bsp;at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)

          at java.security.AccessController.doPrivileged(Native Method)

          at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408)

          at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)

          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 sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)

          at sun.rmi.transport.Transport$1.run(Transport.java:200)

          at sun.rmi.transport.Transport$1.run(Transport.java:197)

          at java.security.AccessController.doPrivileged(Native Method)

          at sun.rmi.transport.Transport.serviceCall(Transport.java:196)

          at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)

          at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)

          at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)

          at java.security.AccessController.doPrivileged(Native Method)

          at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)

          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

          at java.util.concurrent.ThreadPoolExecwww.devze.comutor$Worker.run(ThreadPoolExecutor.java:624)

          at java.lang.Thread.run(Thread.java:748)

      Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException

          at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:217)

          at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)

          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiatpythoneBean(AbstractAutowireCapableBeanFactory.java:1312)

          ... 67 more

      Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException

          at org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter.<init>(AllEncompassingFormHttpMessageConverter.java:76)

          at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.<init>(RequestMappingHandlerAdapter.java:205)

          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

          at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

          at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

          at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:204)

          ... 69 more

      Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException

          at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)

          at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1136)

          ... 76 more

       

      SpringMVC使用jackson解析数据报错

      SpringMVC使用jackson解析json格式数据的时候:

      在springmvc.xml里面使用<mvc:annotation-driven />就不用再单独配置处理器映射器。

      已经导入了jar包,controller里面debug能拿到数据,最后页面访问的时候报错:

      406 (Not Acceptable)

      这时你需要看你是不是将所有前端控制器的后缀都配置成了html,如果是html,那么springmvc默认会采用text/html编码。

      所以,后缀使用别的后缀或者,不用后缀就可以了。

      总结

      以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

      0

      上一篇:

      下一篇:

      精彩评论

      暂无评论...
      验证码 换一张
      取 消

      最新开发

      开发排行榜