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

时间:2022-06-06 07:15:31

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