2021-12-29 22:23:34 +08:00
|
|
|
|
package com.xjs.config;
|
2021-12-26 22:14:30 +08:00
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
|
|
|
|
import com.alibaba.fastjson.serializer.ValueFilter;
|
|
|
|
|
|
import com.alibaba.fastjson.support.config.FastJsonConfig;
|
|
|
|
|
|
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
2021-12-27 10:09:39 +08:00
|
|
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
2021-12-26 22:14:30 +08:00
|
|
|
|
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
2021-12-27 10:09:39 +08:00
|
|
|
|
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
2021-12-26 22:14:30 +08:00
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
2021-12-27 17:04:56 +08:00
|
|
|
|
import org.springframework.http.MediaType;
|
2021-12-26 22:14:30 +08:00
|
|
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2021-12-27 17:04:56 +08:00
|
|
|
|
import java.util.Arrays;
|
2021-12-26 22:14:30 +08:00
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @author xiejs
|
|
|
|
|
|
* @desc 全局序列化处理配置
|
|
|
|
|
|
* @create 2021-12-26
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Configuration
|
|
|
|
|
|
public class JsonConfig {
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
public HttpMessageConverters fastJsonHttpMessageConverters() {
|
|
|
|
|
|
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
|
|
|
|
|
|
FastJsonConfig fastJsonConfig = new FastJsonConfig();
|
|
|
|
|
|
List<SerializerFeature> list = new ArrayList<>();
|
|
|
|
|
|
list.add(SerializerFeature.PrettyFormat);
|
|
|
|
|
|
list.add(SerializerFeature.WriteMapNullValue);
|
|
|
|
|
|
list.add(SerializerFeature.WriteNullStringAsEmpty);
|
|
|
|
|
|
list.add(SerializerFeature.WriteNullListAsEmpty);
|
|
|
|
|
|
list.add(SerializerFeature.QuoteFieldNames);
|
|
|
|
|
|
list.add(SerializerFeature.WriteDateUseDateFormat);
|
|
|
|
|
|
list.add(SerializerFeature.DisableCircularReferenceDetect);
|
|
|
|
|
|
list.add(SerializerFeature.WriteBigDecimalAsPlain);
|
2021-12-27 17:04:56 +08:00
|
|
|
|
|
|
|
|
|
|
//返回枚举类型为枚举toString mp通用枚举用到
|
2021-12-27 10:09:39 +08:00
|
|
|
|
list.add(SerializerFeature.WriteEnumUsingToString);
|
2021-12-27 17:04:56 +08:00
|
|
|
|
|
2021-12-26 22:14:30 +08:00
|
|
|
|
fastJsonConfig.setSerializerFeatures(list.toArray(new SerializerFeature[list.size()]));
|
|
|
|
|
|
fastConverter.setFastJsonConfig(fastJsonConfig);
|
|
|
|
|
|
HttpMessageConverter<?> converter = fastConverter;
|
2021-12-27 17:04:56 +08:00
|
|
|
|
|
|
|
|
|
|
//解决远程调用 ---(Content-Type cannot contain wildcard type '*')报错
|
|
|
|
|
|
fastConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8));
|
|
|
|
|
|
|
|
|
|
|
|
//解决mp雪花算法前端精度丢失
|
2021-12-26 22:14:30 +08:00
|
|
|
|
fastJsonConfig.setSerializeFilters(new ValueFilter() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public Object process(Object object, String name, Object value) {
|
|
|
|
|
|
if ((StringUtils.endsWith(name, "Id") || StringUtils.equals(name,"id")) && value != null
|
|
|
|
|
|
&& value.getClass() == Long.class) {
|
|
|
|
|
|
return String.valueOf(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
return new HttpMessageConverters(converter);
|
|
|
|
|
|
}
|
2021-12-27 10:09:39 +08:00
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
public Jackson2ObjectMapperBuilderCustomizer customizer(){
|
|
|
|
|
|
return builder -> builder.featuresToEnable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-26 22:14:30 +08:00
|
|
|
|
}
|