美文网首页
Json-Spring :No converter found

Json-Spring :No converter found

作者: TeaR0502 | 来源:发表于2017-08-23 12:51 被阅读0次

    最近配置新的Spring-SpringMvc-MyBatis项目时,测试从controller到数据库都是成功的,唯独回传到页面的时候

    报错

    提示

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

    原因是没有注册JSon转换,@ResponseBody时无法根据转换器进行转换

    只要在applicationContext.xml中配置一下即可

    <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>   
    

    需要jackson的jar包

    maven依赖如下
    可以配置在pom.xml中,也可以在中心仓库下载手动导入

    <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>
    

    相关文章

      网友评论

          本文标题:Json-Spring :No converter found

          本文链接:https://www.haomeiwen.com/subject/fiyddxtx.html