这篇文章将为大家详细讲解有关使用SpringBoot怎么对Fastjson于Gson进行整合,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
一、SpringBoot整合Gson
1、pom依赖
# 在SpringBoot中给我们自带了json解析器,我们需要移除SpringBoot自带的jackson,在添加Gson依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--移除jackson依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加Gson依赖-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、User实体
/**
* @Create_Author msfh
* @Create_Date 2020-11-25 15:54:15
* @Description User实体
*/
public class User {
private Integer id;
private String name;
private Date birthday;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", birthday=" + birthday +
'}';
}
/**省略set&get**/
}
3、UserController
/**
* @Create_Author msfh
* @Create_Date 2020-11-25 15:55:15
* @Description UserController控制器
*/
@RestController
public class UserController {
@GetMapping("/user")
public List<User> getUsers(){
ArrayList<User> users = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setId(i);
user.setName("msfh-->"+i);
user.setBirthday(new Date());
users.add(user);
}
return users;
}
}
4、WebMvcConfig
# 在之前的一篇博客中有介绍,大家不太明白可以先看一下上一篇博客,这次就不放测试的结果了!
# 在GsonHttpMessageConvertersConfiguration中含有GsonHttpMessageConverter
# 在GsonAutoConfiguration中含有Gson
# 我们可以分别写两个bean去实现gson的配置(GsonHttpMessageConverter或Gson)
# 建议大家没事的话,可以看下源码
/**
* @Create_Author msfh
* @Create_Date 2020-11-25 16:05:56
* @Description WebMvcConfig配置类
*/
@Configuration
public class WebMvcConfig {
//@Bean
//GsonHttpMessageConverter gsonHttpMessageConverter(){
// GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
// converter.setGson(new GsonBuilder().setDateFormat("yyyy-MM-dd").create());
// return converter;
//}
@Bean
Gson gson(){
return new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
}
}
1、pom依赖
# 这个没什么好说的,还是移除自带的Jackson,添加fastjson,不再做过多解释
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--移除jackson依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加fastjson依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.74</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、User实体(上边代码)
3、UserController(上边代码)
4、WebMvcConfig
# 在fastjson中稍微和之前两种不一致
# 在FastJsonHttpMessageConverter找到FastJsonConfig看一下
/**
* @Create_Author msfh
* @Create_Date 2020-11-25 16:05:56
* @Description WebMvcConfig配置类
*/
@Configuration
public class WebMvcConfig {
@Bean
FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy/MM/dd");
converter.setFastJsonConfig(config);
return converter;
}
}
关于使用SpringBoot怎么对Fastjson于Gson进行整合就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。