一、Spring boot默认集成了3种json映射库的支持:
Gson
Jackson
JSON-B
其中默认的是Jackson
二、Spring boot 默认自动配置了类型为ObjectMapper(该类型来自于Jackson)的bean.可以直接使用该bean进行json映射。
如果pom.xml中配置了gson依赖,则spring boot会自动装配一个类型为Gson的bean.

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>

三、上述两个bean的简单使用:

package cn.edu.tju.controller;

import cn.edu.tju.domain.UserInfo;
import cn.edu.tju.service.BasicService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class TestController3 {
@Autowired
ObjectMapper objectMapper;
@Autowired
Gson gson;

@RequestMapping("/test1")
public String getMessage() throws Exception{
Map<String,String> map=new HashMap<>();
map.put("username","amadeus");
map.put("password","liu");
String result=objectMapper.writeValueAsString(map);
return result;
}

@RequestMapping("/test2")
public String getMessage2() throws Exception{
UserInfo userInfo=new UserInfo();
userInfo.setUsername("paul");
userInfo.setPassword("schools");
String result=gson.toJson(userInfo);
return result;
}
}