springboot 版本:

 <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

FastJson版本:

<!--fastJson库-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>

Jackson的序列化方式:

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer { /**
* 配置Jackson的序列化方式
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
converter.setObjectMapper(objectMapper);
converters.add(converter);
converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
}
}

FastJson的序列化方式:

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer { /**
* fastjson的全局序列化方式
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 1、需要先定义一个·convert转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2、添加fastjson的配置信息,比如 是否要格式化返回json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4、将convert添加到converters当中.
converters.add(fastConverter);
}
}

序列化 工具类

Jackson工具类

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows; import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors; /**
* @author: Sam.yang
* @date: 2020/9/15 11:41
* @desc: Jackson 解析器
*/
@NoArgsConstructor
public class JacksonUtil { /**
* 单个对象复制
*/
@SneakyThrows
public static <F, T> T copy(F from, Class<T> destCls) {
if (from == null) {
return null;
} if (from.getClass().equals(destCls)) {
return (T) from;
}
ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return mapper.readValue(mapper.writeValueAsString(from), destCls);
} /**
* 集合复制
*/
public static <F, T> List<T> copyList(List<F> from, Class<T> destCls) {
if (CollectionUtils.isEmpty(from)) {
return Collections.<T>emptyList();
}
if (from.get(0).getClass().equals(destCls)) {
return (List<T>) from;
}
return from.stream().map(f -> copy(f, destCls)).collect(Collectors.toList());
} /**
* Page复制
*/
public static <F, T> IPage<T> copyPage(IPage<F> from, Class<T> destCls) {
IPage<T> to = new Page<>();
if (null != from) {
to.setCurrent(from.getCurrent());
to.setPages(from.getPages());
to.setSize(from.getSize());
to.setTotal(from.getTotal());
List<T> dls = Lists.newArrayList();
if (!CollectionUtils.isEmpty(from.getRecords())) {
dls = from.getRecords().stream().map(f -> copy(f, destCls)).collect(Collectors.toList());
}
to.setRecords(dls);
}
return to;
} /**
* Page复制到集合 这里用的是mybatisplus分页
*/
public static <F, T> List<T> copyPageToList(IPage<F> from, Class<T> destCls) {
List<T> dls = Lists.newArrayList();
if (null != from) {
if (!CollectionUtils.isEmpty(from.getRecords())) {
dls = from.getRecords().stream().map(f -> copy(f, destCls)).collect(Collectors.toList());
}
}
return dls;
}
}

FastJson序列化工具类

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists; import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors; public class BeanCopyUtil {
private BeanCopyUtil() { }
//单个对象复制
public static <F, T> T copy(F from, Class<T> destCls) {
if (from == null) {
return null;
} if (from.getClass().equals(destCls)) {
return (T) from;
}
return JSON.parseObject(JSON.toJSONString(from), destCls);
}
private static ValueFilter filter = (obj, s, v) -> {
if (v == null)
return "";
return v;
}; //集合复制
public static <F, T> List<T> copyList(List<F> from, Class<T> destCls) {
if (CollectionUtils.isEmpty(from)) {
return Collections.<T>emptyList();
}
if (from.get(0).getClass().equals(destCls)) {
return (List<T>) from;
}
return from.stream().map(f -> copy(f, destCls)).collect(Collectors.toList());
}
//Page复制
public static <F, T> IPage<T> copyPage(IPage<F> from, Class<T> destCls) {
IPage<T> to = new Page<>();
if (null != from){
to.setCurrent(from.getCurrent());
to.setPages(from.getPages());
to.setSize(from.getSize());
to.setTotal(from.getTotal());
List<T> dls = Lists.newArrayList();
if (!CollectionUtils.isEmpty(from.getRecords())) {
dls = from.getRecords().stream().map(f -> copy(f, destCls)).collect(Collectors.toList());
}
to.setRecords(dls);
}
return to;
}
//Page复制到集合
public static <F, T> List<T> copyPageToList(IPage<F> from, Class<T> destCls) {
List<T> dls = Lists.newArrayList();
if (null != from){
if (!CollectionUtils.isEmpty(from.getRecords())) {
dls = from.getRecords().stream().map(f -> copy(f, destCls)).collect(Collectors.toList());
}
}
return dls;
}
}

【Springboot】FastJson与Jackson全局序列化方式的配置和相关工具类的更多相关文章

  1. SpringBoot项目使用RedisTemplate设置序列化方式

    前端时间新项目使用SpringBoot的RedisTemplate遇到一个问题,先简单描述一下问题:不同项目之间redis共用一个,但是我们新项目读不到老项目存储的缓存.新项目搭建的时候没有跟老项目使 ...

  2. 授权过期后AJAX操作跳转到登录页的一种全局处理方式

    前两天园友JustRun分享了一篇 <菜鸟程序员之Asp.net MVC Session过期异常的处理>博文,正好自己前段时间被安排处理过这个问题,发现JustRun的方法有一点点可优化的 ...

  3. SpringBoot修改Redis序列化方式

    前言 由于Springboot默认提供了序列化方式并不是非常理想,对于高要求的情况下,序列化的速度和序列化之后大小有要求的情况下,不能满足,所以可能需要更换序列化的方式. 这里主要记录更换序列化的方式 ...

  4. springboot系列十一、redisTemplate和stringRedisTemplate对比、redisTemplate几种序列化方式比较

    一.redisTemplate和stringRedisTemplate对比 RedisTemplate看这个类的名字后缀是Template,如果了解过Spring如何连接关系型数据库的,大概不会难猜出 ...

  5. 三种序列化方式存取redis的方法

    常见的的序列化反序列方式的效率: protoBuf(PB) > fastjson > jackson > hessian > xstream > java 数据来自于:h ...

  6. Redis 序列化方式StringRedisSerializer、FastJsonRedisSerializer和KryoRedisSerializer

    当我们的数据存储到Redis的时候,我们的键(key)和值(value)都是通过Spring提供的Serializer序列化到数据库的.RedisTemplate默认使用的是JdkSerializat ...

  7. Springboot 关于日期时间格式化处理方式总结

    项目中使用LocalDateTime系列作为DTO中时间的数据类型,但是SpringMVC收到参数后总报错,为了配置全局时间类型转换,尝试了如下处理方式. 注:本文基于Springboot2.x测试, ...

  8. SpringBoot整合reids之JSON序列化文件夹操作

    前言 最近在开发项目,用到了redis作为缓存,来提高系统访问速度和缓解系统压力,提高用户响应和访问速度,这里遇到几个问题做一下总结和整理 快速配置 SpringBoot整合redis有专门的场景启动 ...

  9. JSON 解析 (三)—— FastJSON与Jackson比较

    一.方便性与性能 调用方便性而言: FastJSON提供了大量静态方法,调用简洁方便 Jackson须实例化类,调用相对繁琐,可通过封装成JSON工具类简化调用 性能而言: FastJSON反序列化的 ...

随机推荐

  1. DatePicker 多时间粒度选择器组件

    使用方法: 在.vue文件引入 import ruiDatePicker from '@/components/rattenking-dtpicker/rattenking-dtpicker.vue' ...

  2. 洛谷 P4747 [CERC2017]Intrinsic Interval 线段树维护连续区间

    题目描述 题目传送门 分析 考虑对于 \([l,r]\),如何求出包住它的长度最短的好区间 做法就是用一个指针从 \(r\) 向右扫,每次查询以当前指针为右端点的最短的能包住 \([l,r]\) 的好 ...

  3. $nextTick解决Vue组件卸载在加载合并的问题

    情况是这样的,父子组件都是复选框,点击父组件的复选框,子组件的复选框要显示并全选,取消复选框,子组件隐藏.子组件显隐我用的 v-if ,使用created钩子函数来使子组件处于全选状态. 但是出现的问 ...

  4. 浅析MyBatis(一):由一个快速案例剖析MyBatis的整体架构与运行流程

    MyBatis 是轻量级的 Java 持久层中间件,完全基于 JDBC 实现持久化的数据访问,支持以 xml 和注解的形式进行配置,能灵活.简单地进行 SQL 映射,也提供了比 JDBC 更丰富的结果 ...

  5. C#开发BIMFACE系列35 服务端API之模型对比6:获取模型构建对比分类树

    系列目录     [已更新最新开发文章,点击查看详细] BIMFACE平台提供了服务端"获取模型对比构件分类树"API.目录树返回结果以树状层级关系显示了增删改的构件信息,里面无法 ...

  6. 循环单链表定义初始化及创建(C语言)

    #include <stdio.h> #include <stdlib.h> /** * 含头节点循环单链表定义,初始化 及创建 */ #define OK 1; #defin ...

  7. 【codeforces - 1307G】Cow and Exercise

    目录 description solution accepted code details description 给定 n 点 m 边简单有向图,有边权. q 次询问,每次给出 xi.可以增加某些边 ...

  8. PTE 准备之 Describe Image

    25s 准备时间:决定用什么模板,用模板cover那些信息点 Content: 数字和文字哪个多,就多说哪个,均匀覆盖 Fluency : 保持流利度 不要纠结时态,单复数,人称代词等 时间要求: 尽 ...

  9. springboot 配置文件application

    application.properties # ----------------------------------------# 核心属性# --------------------------- ...

  10. Flutter 改善套娃地狱问题(仿喜马拉雅PC页面举例)

    前言 这篇文章是我一直以来很想写的一篇文章,终于下定决心动笔了. 写Flutter的小伙伴可能都感受到了:掘金的一些热门的Flutter文章下,知乎的一些Flutter的话题下或者一些论坛里面,喷Fl ...