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