update 优化 jackson 自动配置 改为使用 BeanPostProcessor 匿名处理

pull/2/head
疯狂的狮子li 2021-06-13 15:19:38 +08:00
parent 33120e1dce
commit b0dbf45499
1 changed files with 27 additions and 13 deletions

View File

@ -5,31 +5,45 @@ import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeSerializer;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule; 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.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@Slf4j
@Configuration @Configuration
public class YudaoJacksonAutoConfiguration { public class YudaoJacksonAutoConfiguration {
@Bean @Bean
@SuppressWarnings("InstantiationOfUtilityClass") public BeanPostProcessor objectMapperBeanPostProcessor() {
public JsonUtils jsonUtils(ObjectMapper objectMapper) { return new BeanPostProcessor() {
SimpleModule simpleModule = new SimpleModule(); @Override
/* public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
* 1. Long2^53-1JSLong if (!(bean instanceof ObjectMapper)) {
* 2. LocalDateTime return bean;
*/ }
simpleModule ObjectMapper objectMapper = (ObjectMapper) bean;
SimpleModule simpleModule = new SimpleModule();
/*
* 1. Long2^53-1JSLong
* 2. LocalDateTime
*/
simpleModule
// .addSerializer(Long.class, ToStringSerializer.instance) // .addSerializer(Long.class, ToStringSerializer.instance)
// .addSerializer(Long.TYPE, ToStringSerializer.instance) // .addSerializer(Long.TYPE, ToStringSerializer.instance)
.addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE) .addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE)
.addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); .addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE);
objectMapper.registerModules(simpleModule); objectMapper.registerModules(simpleModule);
JsonUtils.init(objectMapper); JsonUtils.init(objectMapper);
return new JsonUtils(); log.info("初始化 jackson 自动配置");
return bean;
}
};
} }
} }