1.引入maven依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.3</version>
</dependency>
maven依赖
2.配置注解
<!-- 使用默认的注解映射 -->
<mvc:annotation-driven />
注解配置
3.使用@ResponseBody
@RequestMapping("/getJson")
@ResponseBody
public Object getJson(){
User user = new User();
user.setId("123");
user.setName("里哈");
return user;
}
代码示例