use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project charon by harvies.
the class FastJsonAutoConfiguration method fastJsonHttpMessageConverter.
@Bean
@ConditionalOnMissingBean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter(FastJsonConfig config) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setFastJsonConfig(config);
// 处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON);
converter.setSupportedMediaTypes(fastMediaTypes);
converter.setFastJsonConfig(config);
return converter;
}
use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project orion-ops by lijiahangmax.
the class JsonSerializerConfig method fastJsonHttpMessageConverters.
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
List<MediaType> mediaTypes = Lists.newList();
mediaTypes.add(MediaType.APPLICATION_JSON);
fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes);
fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.IgnoreNonFieldGetter);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
return new HttpMessageConverters(fastJsonHttpMessageConverter);
}
use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project spring-boot-web by wangchengming666.
the class WebServletConfig method fastJsonHttpMessageConverters.
@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverters() {
// 1.需要先定义一个Convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如:是否要格式化返回就送数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
// SerializerFeature 描述
// WriteNullListAsEmpty 将Collection类型字段的字段空值输出为[]
// WriteNullStringAsEmpty 将字符串类型字段的空值输出为空字符串 ""
// WriteNullNumberAsZero 将数值类型字段的空值输出为0
// WriteNullBooleanAsFalse 将Boolean类型字段的空值输出为false
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteDateUseDateFormat);
// 解决Long转json精度丢失的问题
SerializeConfig serializeConfig = SerializeConfig.globalInstance;
serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
serializeConfig.put(Long.class, ToStringSerializer.instance);
serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
fastJsonConfig.setSerializeConfig(serializeConfig);
// 3.在Convert中添加配置信息;
fastConverter.setFastJsonConfig(fastJsonConfig);
return fastConverter;
}
use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project spring-boot-web by wangchengming666.
the class ApiServletConfig method fastJsonHttpMessageConverters.
@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverters() {
// 1.需要先定义一个Convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如:是否要格式化返回就送数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
// SerializerFeature 描述
// WriteNullListAsEmpty 将Collection类型字段的字段空值输出为[]
// WriteNullStringAsEmpty 将字符串类型字段的空值输出为空字符串 ""
// WriteNullNumberAsZero 将数值类型字段的空值输出为0
// WriteNullBooleanAsFalse 将Boolean类型字段的空值输出为false
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteDateUseDateFormat);
// 解决Long转json精度丢失的问题
SerializeConfig serializeConfig = SerializeConfig.globalInstance;
serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
serializeConfig.put(Long.class, ToStringSerializer.instance);
serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
fastJsonConfig.setSerializeConfig(serializeConfig);
// 3.在Convert中添加配置信息;
fastConverter.setFastJsonConfig(fastJsonConfig);
return fastConverter;
}
use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project JBM by numen06.
the class FastJsonConfiguration method getFastJsonConverter.
@Bean
public FastJsonHttpMessageConverter getFastJsonConverter() {
// 创建fastJson消息转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 升级最新版本需加=============================================================
List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
supportedMediaTypes.add(MediaType.APPLICATION_PDF);
supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
supportedMediaTypes.add(MediaType.APPLICATION_XML);
supportedMediaTypes.add(MediaType.IMAGE_GIF);
supportedMediaTypes.add(MediaType.IMAGE_JPEG);
supportedMediaTypes.add(MediaType.IMAGE_PNG);
supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
supportedMediaTypes.add(MediaType.TEXT_HTML);
supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
supportedMediaTypes.add(MediaType.TEXT_PLAIN);
supportedMediaTypes.add(MediaType.TEXT_XML);
fastConverter.setSupportedMediaTypes(supportedMediaTypes);
// ParserConfig parserConfig = ParserConfig.getGlobalInstance();
// parserConfig.putDeserializer(Enum.class, new EnumValueDeserializer(Enum.class));
//
// SerializeConfig globalInstance = SerializeConfig.getGlobalInstance();
// globalInstance.put(Enum.class, new EnumValueSerializer());
// 创建配置类
FastJsonConfig fastJsonConfig = new FastJsonConfig();
// 设置枚举
// fastJsonConfig.setParserConfig(new EnumParserConfig());
// fastJsonConfig.setSerializeConfig(new EnumSerializeConfig());
// 设置swagger ui
SerializeConfig serializeConfig = fastJsonConfig.getSerializeConfig();
try {
Class jsonClass = ClassUtil.loadClass("springfox.documentation.spring.web.json.Json");
serializeConfig.put(jsonClass, SwaggerJsonSerializer.instance);
} catch (Exception e) {
}
serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
serializeConfig.put(Long.class, ToStringSerializer.instance);
serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
// 修改配置返回内容的过滤
// WriteNullListAsEmpty :List字段如果为null,输出为[],而非null
// WriteNullStringAsEmpty : 字符类型字段如果为null,输出为"",而非null
// DisableCircularReferenceDetect :消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
// WriteNullBooleanAsFalse:Boolean字段如果为null,输出为false,而非null
// WriteMapNullValue:是否输出值为null的字段,默认为false
fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNonStringValueAsString);
fastConverter.setFastJsonConfig(fastJsonConfig);
return fastConverter;
}
Aggregations