Skip to content

HTTP client

Mariusz Kopylec edited this page Feb 7, 2023 · 7 revisions

The WebMVC module uses RestTemplate to forward requests. Its timeouts and underlying HTTP client can be configured. To set a different HTTP client ClientHttpRequestFactoryCreator needs to be implemented and ClientHttpRequestFactoryCreatorConfigurer extended. The default RestTemplate configuration looks like below:

import static com.github.mkopylec.charon.configuration.CharonConfigurer.charonConfiguration;
import static com.github.mkopylec.charon.forwarding.OkClientHttpRequestFactoryCreatorConfigurer.okClientHttpRequestFactoryCreator;
import static com.github.mkopylec.charon.forwarding.RestTemplateConfigurer.restTemplate;
import static com.github.mkopylec.charon.forwarding.TimeoutConfigurer.timeout;

@Configuration
class CharonConfiguration {

    @Bean
    CharonConfigurer charonConfigurer() {
        return charonConfiguration()
                .set(restTemplate()
                        .set(timeout().connection(ofMillis(100)).read(ofMillis(1000)).write(ofMillis(500)))
                        .set(okClientHttpRequestFactoryCreator().httpClient(new Builder()
                                .followRedirects(false)
                                .followSslRedirects(false)))
                        .set(List<ClientHttpRequestInterceptor>));
    }
}

The WebFlux module uses WebClient to forward requests. Its timeouts and underlying HTTP client can be configured. To set a different HTTP client ClientHttpConnectorCreator needs to be implemented and ClientHttpConnectorCreatorConfigurer extended. The default WebClient configuration looks like below:

import static com.github.mkopylec.charon.configuration.CharonConfigurer.charonConfiguration;
import static com.github.mkopylec.charon.forwarding.ReactorClientHttpConnectorCreatorConfigurer.reactorClientHttpConnectorCreator;
import static com.github.mkopylec.charon.forwarding.TimeoutConfigurer.timeout;
import static com.github.mkopylec.charon.forwarding.WebClientConfigurer.webClient;

@Configuration
class CharonConfiguration {

    @Bean
    CharonConfigurer charonConfigurer() {
        return charonConfiguration()
                .set(webClient()
                        .set(timeout().connection(ofMillis(100)).read(ofMillis(1000)).write(ofMillis(500)))
                        .set(reactorClientHttpConnectorCreator().httpClient(create()
                                .followRedirect(false)))
                        .set(List<ExchangeFilterFunction>)
                        .set(ExchangeStrategies));
    }
}

<< previous | next >>