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;
}
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;
}
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);
}
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);
}
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;
}
Aggregations