springmvc java对象无法返回json格式问题

话不多说,三部:

一、在pom.xml引入jakcson

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

注意springmvc的版本,我这边用的是<spring-version>4.3.4.RELEASE</spring-version>

二、在spring的配置文件添加如下注解

<mvc:annotation-driven>
<!-- <mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</mvc:message-converters> -->
</mvc:annotation-driven>

三、在controller请求头上添加@ResponseBody

1
2
3
4
5
6
@RequestMapping(value="get/{id}",method=RequestMethod.GET)
@ResponseBody
public Area getAreaById(@PathVariable("id") Long id) {
    Area area = dao.queryAreaById(id);
    return area;
}

这样java对象就能返回json格式数据了.

java对象可以是实体类,集合类等等

posted @   蜗牛滑板鞋  阅读(938)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示