SpringMVC 写接口 No converter found for return value of type

本文解决了一个常见的Spring MVC问题,即返回HashMap类型数据时出现“No converter found for return value of type: class java.util.HashMap”错误。通过调整配置文件和使用正确的Jackson版本,确保了后台能正确返回JSON格式的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


问题:

运行后,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

重新导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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序邦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值