SpringBoot整合Gson 整合Fastjson

2023-11-15

SpringBoot整合Gson 整合Fastjson

一、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();
    }

}

二、SpringBoot整合FastJson

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;
    }

}

5、测试(无问题)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringBoot整合Gson 整合Fastjson 的相关文章

随机推荐

  • 【C语言基础】学生成绩管理系统(方法:数组)

    涉及 字符型数组 代码 include
  • 两款免费、好用的数据库连接工具

    一 Navicate Navicat是一套快速 可靠的数据库管理工具 专为简化数据库的管理及降低系统管理成本而设 它的设计符合数据库管理员 开发人员及中小企业的需要 Navicat 是以直觉化的图形用户界面而建的 让你可以以安全并且简单的方
  • 《5分钟说完一个概念》:什么是Bootstrap采用

    想知道中国人的平均身高 群体均值 群体方差为 每次抽样 1000 人 抽样了 次 每次抽样的 1000人 的平均身高是一次随机抽样 这
  • 网络带宽和吞吐量

    转载网络带宽和吞吐量
  • crmeb 多商户小程序配置

    一 小程序下载并提交审核 下载微信小程序代码 位置 平台后台 gt 应用 gt 小程序 1 首先需要填写这里的小程序信息 然后在这里就可以下载 如果未开启直播要选择 否则会无法使用 2 下载编译后的小程序代码 没有配置小程序的需要先配置小程
  • 计算机视觉实验:直方图均衡化、Hough变化应用

    实验结果
  • npm指令执行前执行自定义代码

    1 基本逻辑 npm start执行前执行node bin wb handle scripts wb node bin wb handle npm start wb handle源码 删除deleteNodeModules配置的最后一级目录
  • OA项目之项目整体业务分析【附项目原型图演示】

    Welcome Huihui s Code World 接下来看看由辉辉所写的关于OA项目的相关操作吧 目录 Welcome Huihui s Code World 一 项目简介 1 目的 2 人员名词解释 3 主界面展示 二 会议管理 1
  • UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position

    爬虫爬取网页 将其写入文档 出错 错误如下 coding utf 8 和数据流IO操作时的编码转化都已经在程序中加入了 gbk哪里来的呢 其实就是win中新文件的默认编码是gbk 当我们遇到编码问题的时候 有三个地方需要注意的 文件的编码
  • 计算机网络:计算机网络拓扑结构的五种分类方式

    计算机网络拓扑分类 计算机网络的拓扑结构指网络节点和链路之间的分布和互连形成的物理形状 分类一 星形拓扑结构 一种以中央节点为中心 把若干外围节点连接起来的辐射状互联结构 优点 易扩充 控制简单 中央节点实施对全网的控制 缺点 中央节点会成
  • 数据拟合

    数据拟合 MATLAB实现RBF径向基神经网络多输入数据拟合 目录 数据拟合 MATLAB实现RBF径向基神经网络多输入数据拟合 基本介绍 程序设计 模型差异 参考资料 基本介绍 RBF神将网络是一种三层神经网络 其包括输入层 隐层 输出层
  • 维基百科 MediaWiki API 解析

    使用开放的 API 做一个自己的小项目 是一个很好的学习方法 但好像开放的 API 选择并不多 这里给大家多一个选择 简单介绍一下维基百科使用的 MediaWiki API 简介 先简单介绍几个容易混淆的概念 Wiki Wiki 是一种在网
  • 虚拟机 安装Centos 7 详细教程

    1 下载镜像文件centos 7 2 新建centos7目录 将下载的镜像放到这里 方便以后管理 3 打开vm应用软件 点击新建虚拟机 4 选择install centos7进行安装 回车继续 5 选择语言 根据自己爱好 6 在这我们可以选
  • 使用Python批量拼接图片

    前言 当需要将多张图像拼接成一张更大的图像时 通常会用到图片拼接技术 这种技术在许多领域中都有广泛的应用 例如计算机视觉 图像处理 卫星图像 地理信息系统等等 在实际应用中 拼接图像可以用于创建全景图像 地图 海报 广告牌等等 本文将使用以
  • Python中if __name__ == '__main__'的使用方法

    转 如何简单地理解Python中的if name main 2017年09月09日 22 35 42 Locutus 阅读数 219985 1 摘要 通俗的理解 name main 假如你叫小明 py 在朋友眼中 你是小明 name 小明
  • C语言内存函数(memcpy、memmove、memcmp)详解

    一 memcpy string h 1 介绍 memcpy函数为内存拷贝函数 既可以拷贝字符串 也可以拷贝整形数组 浮点型数组等 具有明显的应用优势 destination为目的地空间 source为不可修改 const 的来源空间 num
  • npm WARN saveError ENOENT: no such file or directory解决

    安装完成node js后使用npm安装vue报错如下 C Users lxz gt npm uninstall vueWcsp npm WARN saveError ENOENT no such file or directory open
  • 算法优化

    算法优化 MATLAB实现BO RF贝叶斯优化随机森林算法 目录 算法优化 MATLAB实现BO RF贝叶斯优化随机森林算法 效果一览 基本介绍 模型结构 程序设计 学习总结 参考资料 效果一览 基本介绍 针对集成学习参数众多 缺乏高效准确
  • Rocky Linux ISO国内镜像下载

    Rocky Linux 是 CentOS 的一个分支 它位于 Red Hat Enterprise Linux RHEL 的下游 与 CentOS 一样 它提供了非常适合服务器的稳定版 Linux 它旨在作为 CentOS 的完全兼容替代品
  • SpringBoot整合Gson 整合Fastjson

    SpringBoot整合Gson 整合Fastjson 一 SpringBoot整合Gson 1 pom依赖 在SpringBoot中给我们自带了json解析器 我们需要移除SpringBoot自带的jackson 在添加Gson依赖