Search in sources :

Example 26 with FastJsonHttpMessageConverter

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

the class RestTemplateHelper method getDefaultRestTemplate.

private RestTemplate getDefaultRestTemplate(int connectTimeoutSeconds, int readTimeoutSeconds) {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    requestFactory.setConnectTimeout(connectTimeoutSeconds * 1000);
    requestFactory.setReadTimeout(readTimeoutSeconds * 1000);
    // HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
    // httpRequestFactory.setConnectionRequestTimeout(connectTimeoutSeconds * 1000);
    // httpRequestFactory.setConnectTimeout(readTimeoutSeconds * 1000);
    // httpRequestFactory.setReadTimeout(readTimeoutSeconds * 1000);
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    // 换上fastjson
    List<HttpMessageConverter<?>> httpMessageConverterList = restTemplate.getMessageConverters();
    Iterator<HttpMessageConverter<?>> iterator = httpMessageConverterList.iterator();
    if (iterator.hasNext()) {
        HttpMessageConverter<?> converter = iterator.next();
        // 原有的String是ISO-8859-1编码 去掉
        if (converter instanceof StringHttpMessageConverter) {
            iterator.remove();
        }
        // 由于系统中默认有jackson 在转换json时自动会启用  但是我们不想使用它 可以直接移除或者将fastjson放在首位
        if (converter instanceof GsonHttpMessageConverter || converter instanceof MappingJackson2HttpMessageConverter) {
            iterator.remove();
        }
    }
    httpMessageConverterList.add(new StringHttpMessageConverter(Charset.forName("utf-8")));
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON);
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    httpMessageConverterList.add(0, fastJsonHttpMessageConverter);
    return restTemplate;
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ArrayList(java.util.ArrayList) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter)

Example 27 with FastJsonHttpMessageConverter

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

the class RestTemplateHelper method getHttpsDefaultRestTemplate.

private RestTemplate getHttpsDefaultRestTemplate(int connectTimeoutSeconds, int readTimeoutSeconds) {
    HttpsClientRequestFactory requestFactory = new HttpsClientRequestFactory();
    requestFactory.setConnectTimeout(connectTimeoutSeconds * 1000);
    requestFactory.setReadTimeout(readTimeoutSeconds * 1000);
    // HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
    // httpRequestFactory.setConnectionRequestTimeout(connectTimeoutSeconds * 1000);
    // httpRequestFactory.setConnectTimeout(readTimeoutSeconds * 1000);
    // httpRequestFactory.setReadTimeout(readTimeoutSeconds * 1000);
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    // 换上fastjson
    List<HttpMessageConverter<?>> httpMessageConverterList = restTemplate.getMessageConverters();
    Iterator<HttpMessageConverter<?>> iterator = httpMessageConverterList.iterator();
    if (iterator.hasNext()) {
        HttpMessageConverter<?> converter = iterator.next();
        // 原有的String是ISO-8859-1编码 去掉
        if (converter instanceof StringHttpMessageConverter) {
            iterator.remove();
        }
        // 由于系统中默认有jackson 在转换json时自动会启用  但是我们不想使用它 可以直接移除或者将fastjson放在首位
        if (converter instanceof GsonHttpMessageConverter || converter instanceof MappingJackson2HttpMessageConverter) {
            iterator.remove();
        }
    }
    httpMessageConverterList.add(new StringHttpMessageConverter(Charset.forName("utf-8")));
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON);
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    httpMessageConverterList.add(0, fastJsonHttpMessageConverter);
    return restTemplate;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ArrayList(java.util.ArrayList) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter)

Example 28 with FastJsonHttpMessageConverter

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

the class ConfigurerAdapter method configureMessageConverters.

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    // 使用 fastjson 序列化,会导致 @JsonIgnore 失效,可以使用 @JSONField(serialize = false) 替换
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    List<MediaType> supportMediaTypeList = new ArrayList<>();
    supportMediaTypeList.add(MediaType.APPLICATION_JSON);
    FastJsonConfig config = new FastJsonConfig();
    config.setDateFormat("yyyy-MM-dd HH:mm:ss");
    config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
    converter.setFastJsonConfig(config);
    converter.setSupportedMediaTypes(supportMediaTypeList);
    converter.setDefaultCharset(StandardCharsets.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 29 with FastJsonHttpMessageConverter

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

the class Application method fastJsonHttpMessageConverters.

@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    // 1.定义一个converters转换消息的对象
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    // 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
    // 3.在converter中添加配置信息
    fastConverter.setFastJsonConfig(fastJsonConfig);
    // 5.返回HttpMessageConverters对象
    return new HttpMessageConverters(fastConverter);
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) HttpMessageConverters(org.springframework.boot.autoconfigure.http.HttpMessageConverters) Bean(org.springframework.context.annotation.Bean)

Example 30 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project bbq-ddd by smingjie.

the class WebAppConfig method getFastJsonHttpMessageConverter.

// 自定义fast-json消息转换器,springboot2会自动添加
@Bean
public FastJsonHttpMessageConverter getFastJsonHttpMessageConverter() {
    // 创建消息转换器
    FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
    // 配置类
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(// List字段如果为null,输出为[],而非null
    SerializerFeature.WriteNullListAsEmpty, // 字符类型字段如果为null,输出为"",而非null
    SerializerFeature.WriteNullStringAsEmpty, // Boolean字段如果为null,输出为falseJ,而非null
    SerializerFeature.WriteNullBooleanAsFalse, // 消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
    SerializerFeature.DisableCircularReferenceDetect, // 是否输出值为null的字段,默认为false
    SerializerFeature.WriteMapNullValue);
    fastJsonConfig.setDateFormat(DatePattern.NORM_DATETIME_PATTERN);
    // 媒体类型
    List<MediaType> mediaTypeList = Arrays.asList(MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));
    // 设置到消息转换器
    fastJsonConverter.setSupportedMediaTypes(mediaTypeList);
    log.info("--设置FastJson消息处理器支持的媒体类型:{}", mediaTypeList);
    fastJsonConverter.setFastJsonConfig(fastJsonConfig);
    log.info("--设置FastJson消息处理器全局配置信息:完成");
    return fastJsonConverter;
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) 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