springmvc json类型转换错误解决方案

  

这篇文章主要介绍了springmvc json类型转换错误解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

  

在用springmvc做json数据时,返回时,提示类型转换错误

  

如下,         org.springframework.web.util。NestedServletException:请求处理失败;嵌套的异常. lang。IllegalArgumentException:没有发现转换器的返回值类型:类cn.taotao.bean.Role   org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet.java: 982)   org.springframework.web.servlet.FrameworkServlet.doGet (FrameworkServlet.java: 861)   javax.servlet.http.HttpServlet.service (HttpServlet.java: 634)   org.springframework.web.servlet.FrameworkServlet.service (FrameworkServlet.java: 846)   javax.servlet.http.HttpServlet.service (HttpServlet.java: 741)   org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java: 52)   org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal (CharacterEncodingFilter.java: 197)   org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java: 107)   org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter (ProxiedFilterChain.java: 61)   org.apache.shiro.web.servlet.AdviceFilter.executeChain (AdviceFilter.java: 108)   org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal (AdviceFilter.java: 137)   org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter (OncePerRequestFilter.java: 125)   org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter (ProxiedFilterChain.java: 66)   org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain (AbstractShiroFilter.java: 449)   org.apache.shiro.web.servlet.AbstractShiroFilter 1.美元调用(AbstractShiroFilter.java: 365)   org.apache.shiro.subject.support.SubjectCallable.doCall (SubjectCallable.java: 90)   org.apache.shiro.subject.support.SubjectCallable.call (SubjectCallable.java: 83)   org.apache.shiro.subject.support.DelegatingSubject.execute (DelegatingSubject.java: 383)   org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal (AbstractShiroFilter.java: 362)   org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter (OncePerRequestFilter.java: 125)   org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate (DelegatingFilterProxy.java: 347)   org.springframework.web.filter.DelegatingFilterProxy.doFilter (DelegatingFilterProxy.java: 263)   根本原因      . lang。IllegalArgumentException:没有发现转换器的返回值类型:类cn.taotao.bean.Role   org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters (AbstractMessageConverterMethodProcessor.java: 187)   org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue (RequestResponseBodyMethodProcessor.java: 174)   org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue (HandlerMethodReturnValueHandlerComposite.java: 81)   org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle (ServletInvocableHandlerMethod.java: 113)   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod (RequestMappingHandlerAdapter.java: 854)   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal (RequestMappingHandlerAdapter.java: 765)   org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle (AbstractHandlerMethodAdapter.java: 85)   org.springframework.web.servlet.DispatcherServlet.doDispatch (DispatcherServlet.java: 967)   org.springframework.web.servlet.DispatcherServlet.doService (DispatcherServlet.java: 901)   org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet.java: 970)   org.springframework.web.servlet.FrameworkServlet.doGet (FrameworkServlet.java: 861)   javax.servlet.http.HttpServlet.service (HttpServlet.java: 634)   org.springframework.web.servlet.FrameworkServlet.service (FrameworkServlet.java: 846)   javax.servlet.http.HttpServlet.service (HttpServlet.java: 741)      

解决办法为

  

在pom中添加,只要添加上这两个杰克逊的支持,就可以了。

        & lt; dependency>   & lt; groupId> com.fasterxml.jackson.core   & lt; artifactId> jackson-core   & lt; version> 2.8.8   & lt;/dependency>   & lt; !——返回json字符串的支持——比;   & lt; !——https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind——比;   & lt; dependency>   & lt; groupId> com.fasterxml.jackson.core

springmvc json类型转换错误解决方案