溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

springmvc json類(lèi)型轉(zhuǎn)換錯(cuò)誤解決方案

發(fā)布時(shí)間:2020-09-28 21:47:14 來(lái)源:腳本之家 閱讀:173 作者:琴聲清幽 欄目:編程語(yǔ)言

這篇文章主要介紹了springmvc json類(lèi)型轉(zhuǎn)換錯(cuò)誤解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

在用springmvc做json數(shù)據(jù)時(shí),返回時(shí),提示類(lèi)型轉(zhuǎn)換錯(cuò)誤

如下,

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class 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.call(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)
Root Cause

java.lang.IllegalArgumentException: No converter found for return value of type: class 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中添加,只要添加上這兩個(gè)jackson的支持,就可以了。

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

在springmvc的controller的方法為,如下

@RequestMapping("/getRole/{id}")
  @ResponseBody
  public Role getRole(@PathVariable("id") Integer id ) {
    Role roleById = roleService.getRoleById(id);
    return roleById;
  }

若仍不能解決,可以在springmvc配置文件中進(jìn)行如下配置(待驗(yàn)證)

 <mvc:annotation-driven>   <mvc:message-converters>     <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> 

   <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> 
  </mvc:message-converters> 
</mvc:annotation-driven>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI