Search in sources :

Example 16 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project spring-boot-api-seed-project by selfassu.

the class WebMvcConfigurer method configureMessageConverters.

// 设置 FastJson 作为 Json 对象的转换器
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig config = new FastJsonConfig();
    // 设置转换 json 之后的属性
    // SerializerFeature.WriteMapNullValue 保留空的字段
    config.setSerializerFeatures(SerializerFeature.WriteMapNullValue, // Number 类型为 null 直接转换成 0
    SerializerFeature.WriteNullNumberAsZero, // 字符串类型为 null 直接转换成 ""
    SerializerFeature.WriteNullStringAsEmpty, // 禁止循环引用
    SerializerFeature.DisableCircularReferenceDetect);
    // fastjson 版本过高的时候会报错
    // json java.lang.IllegalArgumentException: 'Content-Type' cannot contain wildcard type '*'
    // 需要自己指定 Content-Type , 老版本中默认指定的是 MediaType.ALL
    List<MediaType> mediaTypes = new ArrayList<>();
    mediaTypes.add(MediaType.APPLICATION_JSON);
    mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    mediaTypes.add(MediaType.APPLICATION_ATOM_XML);
    mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
    mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
    mediaTypes.add(MediaType.APPLICATION_PDF);
    mediaTypes.add(MediaType.APPLICATION_RSS_XML);
    mediaTypes.add(MediaType.APPLICATION_XHTML_XML);
    mediaTypes.add(MediaType.APPLICATION_XML);
    mediaTypes.add(MediaType.IMAGE_GIF);
    mediaTypes.add(MediaType.IMAGE_JPEG);
    mediaTypes.add(MediaType.IMAGE_PNG);
    mediaTypes.add(MediaType.TEXT_EVENT_STREAM);
    mediaTypes.add(MediaType.TEXT_HTML);
    mediaTypes.add(MediaType.TEXT_MARKDOWN);
    mediaTypes.add(MediaType.TEXT_PLAIN);
    mediaTypes.add(MediaType.TEXT_XML);
    converter.setSupportedMediaTypes(mediaTypes);
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    converters.add(converter);
}
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)

Example 17 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project spring-boot-quick by vector4wang.

the class WebConfig method configureMessageConverters.

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    // 序列化配置
    FastJsonConfig config = new FastJsonConfig();
    config.setSerializerFeatures(// 输出key时是否使用双引号
    QuoteFieldNames, // 是否输出值为null的字段
    WriteMapNullValue, // 数值字段如果为null,输出为0,而非null
    WriteNullNumberAsZero, // List字段如果为null,输出为[],而非null
    WriteNullListAsEmpty, // 字符类型字段如果为null,输出为"",而非null
    WriteNullStringAsEmpty, // 禁止循环引用
    DisableCircularReferenceDetect);
    converter.setFastJsonConfig(config);
    converters.add(converter);
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)

Example 18 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project oner365-cloud by xiaozhao32.

the class MessageConverter method fastJsonHttpMessageConverters.

@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    FastJsonHttpMessageConverter fasHttpMessageConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    fasHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON);
    fasHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    return new HttpMessageConverters(fasHttpMessageConverter);
}
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) HttpMessageConverters(org.springframework.boot.autoconfigure.http.HttpMessageConverters) Bean(org.springframework.context.annotation.Bean)

Example 19 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project javaboy-code-samples by lenve.

the class SpringMVCConfig method configureMessageConverters.

@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setCharset(Charset.forName("UTF-8"));
    converter.setFastJsonConfig(fastJsonConfig);
    converters.add(converter);
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)

Example 20 with FastJsonHttpMessageConverter

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

the class WebMvcJsonConfig method fastJsonHttpMessageConverter.

// private static final ThreadLocal<SimpleDateFormat> THREAD_LOCAL = new ThreadLocal<SimpleDateFormat>() {
// @Override
// protected SimpleDateFormat initialValue() {
// return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// }
// };
// fastjson配置方法 由于科学计算法bug 暂时弃用
@Bean
FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig config = new FastJsonConfig();
    config.setSerializerFeatures(// 保留 Map 空的字段
    SerializerFeature.WriteMapNullValue, // 将 String 类型的 null 转成""
    SerializerFeature.WriteNullStringAsEmpty, // 将 Number 类型的 null 转成 0
    SerializerFeature.WriteNullNumberAsZero, // 将 List 类型的 null 转成 []
    SerializerFeature.WriteNullListAsEmpty, // 将 Boolean 类型的 null 转成 false
    SerializerFeature.WriteNullBooleanAsFalse, // 格式化BigDecimal类型
    SerializerFeature.WriteBigDecimalAsPlain, // 格式化日期 -> yyyy-MM-dd HH:mm:ss
    SerializerFeature.WriteDateUseDateFormat, // 避免循环引用
    SerializerFeature.DisableCircularReferenceDetect);
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    List<MediaType> mediaTypeList = new ArrayList<>();
    // 解决中文乱码问题,相当于在 Controller 上的 @RequestMapping 中加了个属性 produces = "application/json"
    mediaTypeList.add(MediaType.APPLICATION_JSON);
    converter.setSupportedMediaTypes(mediaTypeList);
    return converter;
}
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) 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