在 Spring Boot 之后,采用 @RestController 就无需再添加 @ResponseBody 了,因为 Spring Boot 中自带了 Jackson 提供的转换器 MappingJackson2HttpMessageConverter
来实现,即 jackson-databind
。
但是有时候考虑到企业中,最求高效率高运能的状况。有很多工程师会选择使用 Google 提供的 Gson 以及由阿里巴巴开发的、号称目前 JSON 解析速度最快的 Fastjson 框架。接下来将介绍如何将 Spring 默认的转化器改变为这两种转化器。
Gson
Gson 是由 Google 提供的开源 JSON 框架,要使用它就需要先在 pom.xml
中除去原本对于 jackson-databind
的依赖,并同时添加 Gson
的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>