首页 > 编程语言 > 详细

springmvc返回json对象

时间:2019-07-16 22:56:31      阅读:88      评论:0      收藏:0      [点我收藏+]

1.引入jackson的依赖

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.5</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.8.5</version>
</dependency>

2.在spring-mvc.xml中添加

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> 
<bean id="messageAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
    <property name="messageConverters">  
        <list>  
            <!-- Support JSON -->  
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />  
        </list>  
    </property>  
</bean>

默认不支持json数据格式,不添加上面的代码会返回一个406的错误

springmvc返回json对象

原文:https://www.cnblogs.com/gavinYang/p/11198044.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!