All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.boot.autoconfigure.web.client.RestTemplateJdkHttpClientAutoConfiguration Maven / Gradle / Ivy

The newest version!
package org.springframework.boot.autoconfigure.web.client;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.JdkHttpClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.constants.RestTemplateJdkSystemPropertiesConstants;
import org.springframework.web.client.logger.RestTemplateLoggingInterceptor;

import java.util.Optional;

@Configuration
@ConditionalOnClass(value = {java.net.http.HttpClient.class})
@ConditionalOnMissingClass(value = {"org.springframework.cloud.context.config.annotation.RefreshScope"})
@ConditionalOnProperty(name = RestTemplateJdkSystemPropertiesConstants.SPRING_REST_JDK_HTTP_ENABLED, havingValue = "true")
public class RestTemplateJdkHttpClientAutoConfiguration {

    public RestTemplateJdkHttpClientAutoConfiguration() {
        super();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConfigurationProperties(prefix = "spring.rest.jdkhttp")
    protected RestTemplateJdkHttpClientProperties restTemplateJdkHttpClientProperties() {
        return new RestTemplateJdkHttpClientProperties();
    }

    @Bean
    protected RestTemplate restTemplate(RestTemplateJdkHttpClientProperties properties,
        ObjectProvider builderProvider) {
        java.net.http.HttpClient.Builder builder = java.net.http.HttpClient.newBuilder().connectTimeout(properties.getConnectionTimeout())
            .followRedirects(properties.getRedirect()).version(properties.getHttpVersion());
        Optional.ofNullable(properties.getHttp2Priority()).ifPresent(p -> builder.priority(p));
        java.net.http.HttpClient httpClient = builder.build();
        return builderProvider.getIfAvailable(RestTemplateBuilder::new)
            .requestFactory(() -> new JdkHttpClientHttpRequestFactory(httpClient, properties.getReadTimeout()))
            .interceptors(new RestTemplateLoggingInterceptor(properties.getLevel())).build();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy