方式一:jackson
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
测试
@RequestMapping("/json")
@ResponseBody
public String jackSon(){
ObjectMapper objectMapper = new ObjectMapper();
User user = new User("张三",5000,"男");
try {
return objectMapper.writeValueAsString(user);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
前端显示(乱码)
可能出现乱码,在前端,所以需要在Spring MVC的配置文件中配