update 优化 jackson 序列化配置
This commit is contained in:
parent
5311937b69
commit
c2cf7ba16a
|
@ -3,34 +3,48 @@ package com.ruoyi.framework.config;
|
||||||
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 com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||||
|
import com.ruoyi.common.utils.JsonUtils;
|
||||||
|
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 org.springframework.context.annotation.Primary;
|
|
||||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当Mybatis plus设置为雪花ID时
|
* jackson 配置
|
||||||
* 使用此类,会把所有数字返回变为字符串返回适配前端Long型失真问题
|
|
||||||
*
|
*
|
||||||
* @author Ming LI
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Configuration
|
@Configuration
|
||||||
public class JacksonConfig {
|
public class JacksonConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Primary
|
public BeanPostProcessor objectMapperBeanPostProcessor() {
|
||||||
@ConditionalOnMissingBean(ObjectMapper.class)
|
return new BeanPostProcessor() {
|
||||||
@ConditionalOnProperty(value = "mybatis-plus.global-config.dbConfig.idType", havingValue = "ASSIGN_ID")
|
@Override
|
||||||
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
|
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||||
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
|
if (!(bean instanceof ObjectMapper)) {
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
ObjectMapper objectMapper = (ObjectMapper) bean;
|
||||||
// 全局配置序列化返回 JSON 处理
|
// 全局配置序列化返回 JSON 处理
|
||||||
SimpleModule simpleModule = new SimpleModule();
|
SimpleModule simpleModule = new SimpleModule();
|
||||||
//JSON Long ==> String
|
//JSON Long ==> String 把所有数字返回变为字符串返回适配前端Long型失真问题
|
||||||
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
|
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
|
||||||
|
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
|
||||||
|
simpleModule.addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE);
|
||||||
|
simpleModule.addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE);
|
||||||
objectMapper.registerModule(simpleModule);
|
objectMapper.registerModule(simpleModule);
|
||||||
return objectMapper;
|
JsonUtils.init(objectMapper);
|
||||||
|
log.info("初始化 jackson 配置");
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue