1. 创建请求工厂(可选)
@Bean
@ConditionalOnMissingBean({ClientHttpRequestFactory.class})
public ClientHttpRequestFactory requestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
//ms
factory.setConnectTimeout(15000);
factory.setReadTimeout(15000);
return factory;
}
为啥是可选的呢 因为你即使不创建 springboot在检测为空时也会为你创建一个
SimpleClientHttpRequestFactory
类型的工程,当然如果你想使用其他的技术那就要指定了
比如httpclient
当然肯定是需要其他的依赖包的
2. 实例化一个RestTemplate
这个是我我们直接使用的请求类
先上代码
@Bean
@ConditionalOnMissingBean({RestTemplate.class})
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
RestTemplate restTemplate = new RestTemplate(factory);
//换上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.WriteNullStringAsEmpty,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.DisableCircularReferenceDetect);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
httpMessageConverterList.add(0,fastJsonHttpMessageConverter);
return restTemplate;
}
创建
RestTemplate
最简单的方式就是注入一个RestTemplateBuilder
直接.build()
即可 但是这样我们就无法定制了 还要使用fastjson呢
在阅读源码时我们发现转化列表中有个
StringHttpMessageConverter
对象它的默认编码是“ISO-8859-1”,顺手给换个“UTF-8”的吧
由于springboot中集成了jackson所以里边队友json的处理也都是jackson,但是本人不太喜欢用Jackson,那么咱就换成国产的
Fastjson
,这里主要是要将GsonHttpMessageConverter
和FastJsonHttpMessageConverter
两个转换器去除然后加上新的FastJsonHttpMessageConverter
,在源码中我们发现一旦有适合处理的转换器,就不会再找下一个了,这样我们就可以直接将自定义的FastJsonHttpMessageConverter
放在列表的首位就行了
完整源码如下
package com.marry.dezhou.fg.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
/**
* @author ml
* @create 2017-10-27--15:53
*/
@Configuration
public class RestConfiguration {
@Autowired
private RestTemplateBuilder builder;
@Bean
@ConditionalOnMissingBean({RestTemplate.class})
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
//return builder.build();
RestTemplate restTemplate = new RestTemplate(factory);
//换上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.WriteNullStringAsEmpty,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.DisableCircularReferenceDetect);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
httpMessageConverterList.add(0,fastJsonHttpMessageConverter);
return restTemplate;
}
@Bean
@ConditionalOnMissingBean({ClientHttpRequestFactory.class})
public ClientHttpRequestFactory requestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
//ms
factory.setConnectTimeout(15000);
factory.setReadTimeout(15000);
return factory;
}
}