json转换错误:No converter found for return value of type

来源:互联网 发布:网络攻防入门 编辑:程序博客网 时间:2024/06/10 01:36

No converter found for return value of type: class java.util.HashMap

最近在搭建一个spring+springMVC+Mybatis项目,其中spring是4.2.0版本,但是在ajax与后台交互的时候,前台的ajax一直接收不到controller返回来的数据:

  • 代码如下

前台代码(ajax代码块):

function isHandle(id){    alert("ishandle");    $.ajax({        url:"isHandle_change",        type:"post",        dataType:"json",        contentType: "application/json",        data:{id:id},        success:function(result){            alert("测试进入success方法");        }    });}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

后台controller方法:

@RequestMapping("/isHandle_change")@ResponseBodypublic Map<String,Object> complaintListChange(){    List<ComplaintCondition> list=new ArrayList<ComplaintCondition>();    list=complaintService.getComplaintList1();    Map<String,Object> map=new HashMap<String, Object>();    map.put("date",list);    return map;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

问题:

运行后,ajax可以访问controller方法,但是返回的json数据后,ajax的success接收不到,提示的错误是:  
Java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.HashMap

从错误信息的原因来看,是后台返回的json数据与ajax的dataType:”json” 不匹配,但是@responseBody注解会自动将返回数据封装成json格式,猜想是配置的问题,还有json包版本冲突

于是重新配置了一下applicationContext.xml里面的配置:

<mvc:annotation-driven  /><!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">    <property name="messageConverters">        <list>            <!--json转换器-->            <ref bean="mappingJacksonHttpMessageConverter" />        </list>    </property></bean><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">      <property name="messageConverters">          <list>                 <ref bean="mappingJacksonHttpMessageConverter" />          </list>      </property>  </bean>  <bean id="mappingJacksonHttpMessageConverter"      class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">       <property name = "supportedMediaTypes">            <list>              <bean class="org.springframework.http.MediaType">               <constructor-arg index="0" value="text"/>               <constructor-arg index="1" value="plain"/>               <constructor-arg index="2" value="UTF-8"/>              </bean>              <bean class="org.springframework.http.MediaType">               <constructor-arg index="0" value="*"/>               <constructor-arg index="1" value="*"/>               <constructor-arg index="2" value="UTF-8"/>              </bean>              <bean class="org.springframework.http.MediaType">               <constructor-arg index="0" value="text"/>               <constructor-arg index="1" value="*"/>               <constructor-arg index="2" value="UTF-8"/>              </bean>               <bean class="org.springframework.http.MediaType">               <constructor-arg index="0" value="application"/>               <constructor-arg index="1" value="json"/>               <constructor-arg index="2" value="UTF-8"/>              </bean>            </list>      </property>  </bean>   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

重新导2.7版本的jackson相关包: 
jackson-databind-2.7.4.jar 
jackson-annotations-2.7.4.jar 
jackson-core-2.7.4.jar

以上jar包下载地址(拷贝下面URL在新页面打开,不然下载不了): 
http://cdn.sojson.com/file/16-05-03-20-51-11/json-jar.zip

如果是Maven项目,则依赖如下:

<dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-databind</artifactId>    <version>2.7.4</version></dependency><dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-core</artifactId>    <version>2.7.4</version></dependency><dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-annotations</artifactId>    <version>2.7.4</version></dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

最后,困扰我几天的问题解决了。应该是json的包版本问题,如果你也遇到问题。可以先排除配置和包问题这两方面。 
最后,感谢部门的龙哥帮忙,找到了问题的根本。 
也感谢这个相关帖子提供相关配置说明:http://www.sojson.com/blog/133.html

阅读全文
0 0
原创粉丝点击