码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC返回Json失败,请检查是否配置了Jackson

时间:2017-08-07 17:36:19      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:springmvc jackson json 配置文件

背景

需要一套干净的Spring+Spring MVC+Mybatis框架,框架写到返回Json的环节报错了。


问题

无法Json,报406


解决

需要配置Json工具包,这里用Jackson


Maven Jackson

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.6.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.6.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.0</version>
        </dependency>

工程环境SpringMvc 4.x,不兼容Jackson2.9最新版。这里特应用2.6版本


Spring Mvc配置

    <!--Begin:使用Jackson 2.x的配置,需要导入的jar包:jackson-core-xxx.jar、jackson-annotations-xxx.jar、jackson-databind-xxx.jar-->
    <!--通过处理器映射DefaultAnnotationHandlerMapping来开启支持@Controller注解-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
    <!--通过处理器适配器AnnotationMethodHandlerAdapter来开启支持@RequestMapping注解-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <!-- 设置返回字符串编码 -->
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name = "supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
                <!-- json转换器 -->
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
    <!--End:使用Jackson 2.x的配置,需要导入的jar包:jackson-core-xxx.jar、jackson-annotations-xxx.jar、jackson-databind-xxx.jar-->

好了,重启不报错(一般就是jar宝冲突),就可以正常接收到@ResponseBody返回的Json了。


以上配置,带来好运,祝好    !

by.沫沫金

技术分享

我得纯净框架,新模块只需要一个Controller就可以,CRUD一句话全搞定。前台专注Form就行。

本文出自 “沫沫金的IT心得与技巧” 博客,请务必保留此出处http://zl0828.blog.51cto.com/2242565/1954239

SpringMVC返回Json失败,请检查是否配置了Jackson

标签:springmvc jackson json 配置文件

原文地址:http://zl0828.blog.51cto.com/2242565/1954239

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!