从RestTemplate的无参构造可以看出,需要判断类路径下是否有jackson的相关类,有才会添加MappingJackson2HttpMessageConverter,故可以在pom.xml中排除jackson的支持,以springboot项目举例
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>jackson-databind</artifactId> <groupId>com.fasterxml.jackson.core</groupId> </exclusion> </exclusions> </dependency>(2)在初始化配置RestTemplate时,去掉其默认的MappingJackson2HttpMessageConverter
@Bean public RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); restTemplate.setRequestFactory(clientHttpRequestFactory()); //restTemplate默认的HttpMessageConverter List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters(); List<HttpMessageConverter<?>> messageConvertersNew = new ArrayList<HttpMessageConverter<?>>(); for(HttpMessageConverter httpMessageConverter : messageConverters){ //跳过MappingJackson2HttpMessageConverter if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) continue; messageConvertersNew.add(httpMessageConverter); } //添加fastjson转换器 messageConvertersNew.add(fastJsonHttpMessageConverter()); return restTemplate; } @Bean public HttpMessageConverter fastJsonHttpMessageConverter() { //MediaType List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8); //FastJsonConfig FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue, SerializerFeature.QuoteFieldNames); //创建FastJsonHttpMessageConverter4 Spring 4.2后使用 FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter4(); fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes); fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); return fastJsonHttpMessageConverter; } 五、SpringBoot中使用RestTemplateSpringBoot项目可以通过上面的方式,@Bean往Spring容器中注册一个配置好的RestTemplate实例,也可以参考 SpringBoot官方 的方式自定义RestTemplate
由于RestTemplate实例在使用前经常需要自定义,SpringBoot没有提供自动配置好的RestTemplate,但是自动配置好了可以用于创建RestTemplate的RestTemplateBuilder实例,可以按如下使用
@Service public class MyBean { private final RestTemplate restTemplate; public MyBean(RestTemplateBuilder restTemplateBuilder) { this.restTemplate = restTemplateBuilder.build(); } public Details someRestCall(String name) { return this.restTemplate.getForObject("/{name}/details", Details.class, name); } } RestTemplate自定义RestTemplate自定义主要有三种方法,具体取决于希望自定义应用的范围
类范围。为了尽量缩小自定义的范围,在类中注入自动配置的RestTemplateBuilder,然后根据需求调用它的配置方法,每次调用配置方法都会 new RestTemplateBuilder()并返回,所以对RestTemplateBuilder的配置只会影响由它创建的RestTemplate
应用范围。可以使用RestTemplateCustomizer来自定义应用范围的的RestTemplate,所有注册到Spring容器的RestTemplateCustomizer都会自动生效。如下,通过RestTemplateCustomizer设置连接池
@Bean public RestTemplateCustomizer restTemplateCustomizer(){ return new RestTemplateCustomizer(){ @Override public void customize(RestTemplate restTemplate) { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); //创建连接管理器 PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(); poolingHttpClientConnectionManager.setMaxTotal(100); poolingHttpClientConnectionManager.setDefaultMaxPerRoute(20); httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager); //创建httpClient HttpClient httpClient = httpClientBuilder.build(); //创建HttpComponentsClientHttpRequestFactory HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); httpComponentsClientHttpRequestFactory.setConnectTimeout(10 * 1000); httpComponentsClientHttpRequestFactory.setReadTimeout(60 * 1000); httpComponentsClientHttpRequestFactory.setConnectionRequestTimeout(20 * 1000); restTemplate.setRequestFactory(httpComponentsClientHttpRequestFactory); } }; }最后,最极端的(也是很少使用的)选项是创建你自己的RestTemplateBuilder bean。这将关闭RestTemplateBuilder的自动配置,并阻止使用任何RestTemplateCustomizer bean