|
|
|
|
@ -1,51 +1,42 @@
|
|
|
|
|
package cn.iocoder.yudao.framework.jackson.config;
|
|
|
|
|
|
|
|
|
|
import cn.iocoder.yudao.framework.jackson.core.databind.NumberSerializer;
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
|
|
|
|
import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeDeserializer;
|
|
|
|
|
import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeSerializer;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
|
|
|
|
import cn.iocoder.yudao.framework.jackson.core.databind.NumberSerializer;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@AutoConfiguration
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class YudaoJacksonAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public BeanPostProcessor objectMapperBeanPostProcessor() {
|
|
|
|
|
return new BeanPostProcessor() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
|
|
|
if (!(bean instanceof ObjectMapper)) {
|
|
|
|
|
return bean;
|
|
|
|
|
}
|
|
|
|
|
ObjectMapper objectMapper = (ObjectMapper) bean;
|
|
|
|
|
SimpleModule simpleModule = new SimpleModule();
|
|
|
|
|
/*
|
|
|
|
|
* 1. 新增Long类型序列化规则,数值超过2^53-1,在JS会出现精度丢失问题,因此Long自动序列化为字符串类型
|
|
|
|
|
* 2. 新增LocalDateTime序列化、反序列化规则
|
|
|
|
|
*/
|
|
|
|
|
simpleModule
|
|
|
|
|
.addSerializer(Long.class, NumberSerializer.INSTANCE)
|
|
|
|
|
.addSerializer(Long.TYPE, NumberSerializer.INSTANCE)
|
|
|
|
|
.addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE)
|
|
|
|
|
.addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE);
|
|
|
|
|
|
|
|
|
|
objectMapper.registerModules(simpleModule);
|
|
|
|
|
|
|
|
|
|
JsonUtils.init(objectMapper);
|
|
|
|
|
log.info("初始化 jackson 自动配置");
|
|
|
|
|
return bean;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
@SuppressWarnings("InstantiationOfUtilityClass")
|
|
|
|
|
public JsonUtils jsonUtils(List<ObjectMapper> objectMappers) {
|
|
|
|
|
// 1.1 创建 SimpleModule 对象
|
|
|
|
|
SimpleModule simpleModule = new SimpleModule();
|
|
|
|
|
simpleModule
|
|
|
|
|
// 新增 Long 类型序列化规则,数值超过 2^53-1,在 JS 会出现精度丢失问题,因此 Long 自动序列化为字符串类型
|
|
|
|
|
.addSerializer(Long.class, NumberSerializer.INSTANCE)
|
|
|
|
|
.addSerializer(Long.TYPE, NumberSerializer.INSTANCE)
|
|
|
|
|
// 新增 LocalDateTime 序列化、反序列化规则
|
|
|
|
|
.addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE)
|
|
|
|
|
.addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE);
|
|
|
|
|
// 1.2 注册到 objectMapper
|
|
|
|
|
objectMappers.forEach(objectMapper -> objectMapper.registerModule(simpleModule));
|
|
|
|
|
|
|
|
|
|
// 2. 设置 objectMapper 到 JsonUtils {
|
|
|
|
|
JsonUtils.init(CollUtil.getFirst(objectMappers));
|
|
|
|
|
log.info("[init][初始化 JsonUtils 成功]");
|
|
|
|
|
return new JsonUtils();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|