Search in sources :

Example 31 with FastJsonHttpMessageConverter

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;
}
Also used : ArrayList(java.util.ArrayList) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) MediaType(org.springframework.http.MediaType) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 32 with FastJsonHttpMessageConverter

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);
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) MediaType(org.springframework.http.MediaType) HttpMessageConverters(org.springframework.boot.autoconfigure.http.HttpMessageConverters) Bean(org.springframework.context.annotation.Bean)

Example 33 with 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;
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) Bean(org.springframework.context.annotation.Bean)

Example 34 with FastJsonHttpMessageConverter

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;
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) Bean(org.springframework.context.annotation.Bean)

Example 35 with FastJsonHttpMessageConverter

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;
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) ArrayList(java.util.ArrayList) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) MediaType(org.springframework.http.MediaType) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) Bean(org.springframework.context.annotation.Bean)

Aggregations

FastJsonHttpMessageConverter (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)42 FastJsonConfig (com.alibaba.fastjson.support.config.FastJsonConfig)33 Bean (org.springframework.context.annotation.Bean)22 MediaType (org.springframework.http.MediaType)17 ArrayList (java.util.ArrayList)16 HttpMessageConverters (org.springframework.boot.autoconfigure.http.HttpMessageConverters)7 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)5 HttpHeaders (org.springframework.http.HttpHeaders)5 HttpOutputMessage (org.springframework.http.HttpOutputMessage)5 RestTemplate (org.springframework.web.client.RestTemplate)4 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)3 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HttpMessageConverters (org.springframework.boot.autoconfigure.web.HttpMessageConverters)2 HttpInputMessage (org.springframework.http.HttpInputMessage)2 GsonHttpMessageConverter (org.springframework.http.converter.json.GsonHttpMessageConverter)2 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)2 SimpleDateFormatSerializer (com.alibaba.fastjson.serializer.SimpleDateFormatSerializer)1