Search in sources :

Example 36 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project JBM by numen06.

the class RestTemplateFactory method initMessageConverters.

public static void initMessageConverters(RestTemplate restTemplate) {
    restTemplate.getMessageConverters().removeIf(new Predicate<HttpMessageConverter<?>>() {

        @Override
        public boolean test(HttpMessageConverter<?> t) {
            if (t instanceof StringHttpMessageConverter)
                return true;
            if (t instanceof FormHttpMessageConverter) {
                return true;
            }
            return false;
        }
    });
    restTemplate.getMessageConverters().add(1, new StringHttpMessageConverter(Charset.defaultCharset()));
    restTemplate.getMessageConverters().add(fileFormHttpMessageConverter);
    restTemplate.getMessageConverters().add(fastJsonHttpMessageConverter);
}
Also used : FormHttpMessageConverter(org.springframework.http.converter.FormHttpMessageConverter) FormHttpMessageConverter(org.springframework.http.converter.FormHttpMessageConverter) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Example 37 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project jeeagile by jeeagile.

the class AgileWebAutoConfigure method fastJsonHttpMessageConverter.

@Bean
@ConditionalOnMissingBean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.DisableCircularReferenceDetect);
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON);
    fastConverter.setSupportedMediaTypes(fastMediaTypes);
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    fastConverter.setFastJsonConfig(fastJsonConfig);
    return fastConverter;
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) 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 38 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project ebase-boot by ebase-projects.

the class CustomWebMvcConfig method fastJsonHttpMessageConverter.

/**
 * fastjson的配置
 * https://www.jianshu.com/p/02e8db6b6b23
 */
// @Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteEnumUsingToString, SerializerFeature.WriteNullStringAsEmpty);
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    fastJsonConfig.setCharset(Charset.forName("utf-8"));
    // 解决Long转json精度丢失的问题
    SerializeConfig serializeConfig = SerializeConfig.globalInstance;
    serializeConfig.put(BigInteger.class, com.alibaba.fastjson.serializer.ToStringSerializer.instance);
    serializeConfig.put(Long.class, com.alibaba.fastjson.serializer.ToStringSerializer.instance);
    serializeConfig.put(Long.TYPE, com.alibaba.fastjson.serializer.ToStringSerializer.instance);
    // 驼峰转下划线
    // https://github.com/alibaba/fastjson/wiki/PropertyNamingStrategy_cn
    // serializeConfig.setPropertyNamingStrategy(com.alibaba.fastjson.PropertyNamingStrategy.SnakeCase);
    fastJsonConfig.setSerializeConfig(serializeConfig);
    converter.setFastJsonConfig(fastJsonConfig);
    return converter;
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)

Example 39 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project springboot-manager by aitangbao.

the class WebMvcConfigurer method fastJsonHttpMessageConverter.

@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig config = new FastJsonConfig();
    // Long类型转String类型
    SerializeConfig serializeConfig = SerializeConfig.globalInstance;
    // ToStringSerializer 是这个包 com.alibaba.fastjson.serializer.ToStringSerializer
    serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
    serializeConfig.put(Long.class, ToStringSerializer.instance);
    serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
    config.setSerializeConfig(serializeConfig);
    config.setSerializerFeatures(// 保留map空的字段
    SerializerFeature.WriteMapNullValue, // 将String类型的null转成""
    SerializerFeature.WriteNullStringAsEmpty, // 将Number类型的null转成0
    SerializerFeature.WriteNullNumberAsZero, // 将List类型的null转成[]
    SerializerFeature.WriteNullListAsEmpty, // 将Boolean类型的null转成false
    SerializerFeature.WriteNullBooleanAsFalse, // 日期格式转换
    SerializerFeature.WriteDateUseDateFormat, // 避免循环引用
    SerializerFeature.DisableCircularReferenceDetect);
    config.setSerializeFilters(valueFilter);
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(StandardCharsets.UTF_8);
    // 解决中文乱码问题,相当于在Controller上的@RequestMapping中加了个属性produces = "application/json"
    List<MediaType> mediaTypeList = new ArrayList<>();
    mediaTypeList.add(MediaType.APPLICATION_JSON);
    converter.setSupportedMediaTypes(mediaTypeList);
    return converter;
}
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) Bean(org.springframework.context.annotation.Bean)

Example 40 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project spring_boot by hryou0922.

the class MybatisSpringBoot method fastJsonHttpMessageConverters.

@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    // 格式化时间
    SerializeConfig mapping = new SerializeConfig();
    mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    // fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    fastJsonConfig.setSerializeConfig(mapping);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    HttpMessageConverter<?> converter = fastConverter;
    return new HttpMessageConverters(converter);
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) SimpleDateFormatSerializer(com.alibaba.fastjson.serializer.SimpleDateFormatSerializer) HttpMessageConverters(org.springframework.boot.autoconfigure.web.HttpMessageConverters) 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