001/* 002 * Copyright 2012-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.web; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.springframework.beans.factory.ObjectProvider; 023import org.springframework.boot.autoconfigure.AutoConfigureAfter; 024import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 027import org.springframework.boot.web.client.RestTemplateBuilder; 028import org.springframework.boot.web.client.RestTemplateCustomizer; 029import org.springframework.context.annotation.Bean; 030import org.springframework.context.annotation.Configuration; 031import org.springframework.core.annotation.AnnotationAwareOrderComparator; 032import org.springframework.util.CollectionUtils; 033import org.springframework.web.client.RestTemplate; 034 035/** 036 * {@link EnableAutoConfiguration Auto-configuration} for web client. 037 * 038 * @author Stephane Nicoll 039 * @author Phillip Webb 040 * @since 1.4.0 041 */ 042@Configuration 043@AutoConfigureAfter(HttpMessageConvertersAutoConfiguration.class) 044public class WebClientAutoConfiguration { 045 046 @Configuration 047 @ConditionalOnClass(RestTemplate.class) 048 public static class RestTemplateConfiguration { 049 050 private final ObjectProvider<HttpMessageConverters> messageConverters; 051 052 private final ObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers; 053 054 public RestTemplateConfiguration( 055 ObjectProvider<HttpMessageConverters> messageConverters, 056 ObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers) { 057 this.messageConverters = messageConverters; 058 this.restTemplateCustomizers = restTemplateCustomizers; 059 } 060 061 @Bean 062 @ConditionalOnMissingBean 063 public RestTemplateBuilder restTemplateBuilder() { 064 RestTemplateBuilder builder = new RestTemplateBuilder(); 065 HttpMessageConverters converters = this.messageConverters.getIfUnique(); 066 if (converters != null) { 067 builder = builder.messageConverters(converters.getConverters()); 068 } 069 List<RestTemplateCustomizer> customizers = this.restTemplateCustomizers 070 .getIfAvailable(); 071 if (!CollectionUtils.isEmpty(customizers)) { 072 customizers = new ArrayList<RestTemplateCustomizer>(customizers); 073 AnnotationAwareOrderComparator.sort(customizers); 074 builder = builder.customizers(customizers); 075 } 076 return builder; 077 } 078 079 } 080 081}