1.引入maven依赖
1 <dependency> 2 <groupId>com.fasterxml.jackson.core</groupId> 3 <artifactId>jackson-databind</artifactId> 4 <version>2.9.3</version> 5 </dependency> 6 <dependency> 7 <groupId>com.fasterxml.jackson.core</groupId> 8 <artifactId>jackson-core</artifactId> 9 <version>2.9.3</version> 10 </dependency> 11 <dependency> 12 <groupId>com.fasterxml.jackson.core</groupId> 13 <artifactId>jackson-annotations</artifactId> 14 <version>2.9.3</version> 15 </dependency>
2.配置注解
1 <!-- 使用默认的注解映射 --> 2 <mvc:annotation-driven />
3.使用@ResponseBody
1 @RequestMapping("/getJson") 2 @ResponseBody 3 public Object getJson(){ 4 User user = new User(); 5 user.setId("123"); 6 user.setName("里哈"); 7 return user; 8 }