Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestFactory(Class<? extends ClientHttpRequestFactory>) in the type RestTemplateBuilder is not applicable

I am using springBootVersion = '2.0.1.RELEASE' on my project. I am trying to write mutual authentication code for that I wrote RestClientCertTestConfiguration class as below. I am getting error on requestFactory. The method requestFactory(Class) in the type RestTemplateBuilder is not applicable for the arguments (HttpComponentsClientHttpRequestFactory) Any suggestion on how to resolve this issue? thanks

    @Configuration
public class RestClientCertTestConfiguration {

    private String allPassword = "mypassword";

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {

        SSLContext sslContext = SSLContextBuilder
                .create()
                .loadKeyMaterial(ResourceUtils.getFile("classpath:myCerts.jks"), allPassword.toCharArray(), allPassword.toCharArray())
                .loadTrustMaterial(ResourceUtils.getFile("classpath:myCerts.jks"), allPassword.toCharArray())
                .build();

        HttpClient client = HttpClients.custom()
                .setSSLContext(sslContext)
                .build();

        return builder 
                 //error on this line
                .requestFactory(new HttpComponentsClientHttpRequestFactory(client))
                .build();
    }

}
like image 310
Maana Avatar asked Dec 07 '18 19:12

Maana


People also ask

What is RestTemplateBuilder?

public class RestTemplateBuilder extends Object. Builder that can be used to configure and create a RestTemplate . Provides convenience methods to register converters , error handlers and UriTemplateHandlers .

What is HttpComponentsClientHttpRequestFactory?

HttpComponentsClientHttpRequestFactory is ClientHttpRequestFactory implementation that uses Apache HttpComponents HttpClient to create requests. We have used @Scheduled annotation in httpClient configuration. To support this, we have to add support of scheduled execution of thread.

Is it possible to inject clienthttprequestfactory into requestfactory in resttemplatebuilder?

After digging deeper into the source code of RestTemplateBuilder of Spring Boot 2.1.x, I found that they have removed the method requestFactory (ClientHttpRequestFactory requestFactory). That means you can no longer inject the ClientHttpRequestFactory object into requestFactory method.

How to create a requestfactory from a resttemplate in Spring Boot?

So if you have only one restTemplate and one requestFactory, all you need to do is register a HttpComponentsClientHttpRequestFactory bean in Spring context and pass a ClientHttpRequestFactorySupplier to requestFactory method. The supplier will automatically detect your HttpComponentsClientHttpRequestFactory and return you the required RestTemplate.

What is the use of resttemplatebuilder?

Builder that can be used to configure and create a RestTemplate. Provides convenience methods to register converters, error handlers and UriTemplateHandlers . By default the built RestTemplate will attempt to use the most suitable ClientHttpRequestFactory, call detectRequestFactory (false) if you prefer to keep the default.

What version of Spring Boot does the clienthttprequestfactorysupplier class exist?

Additional (and important) info: the ClientHttpRequestFactorySupplier () class exists only in Spring Boot 2.1.0 and above; thus, the solution applies to SpringBoot2.1.x+, and not to 2.x.x as stated. ok but what if you have 2 restTemplate which need 2 distinct "requestFactory" ?


2 Answers

The requestFactory method takes either the class, or a Supplier<ClientHttpRequestFactory> so you need to do either:

.requestFactory(HttpComponentsClientHttpRequestFactory.class)

or

.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client))

Presumably the latter, since you want to pass in client.

like image 110
David Conrad Avatar answered Sep 24 '22 00:09

David Conrad


The below method works fine till spring boot 1.x

.requestFactory(new HttpComponentsClientHttpRequestFactory(client))

but in later version of spring boot like 2.x you need to change it to -

.requestFactory(HttpComponentsClientHttpRequestFactory.class)

or

.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client))
like image 38
Arvind Kumar Avatar answered Sep 22 '22 00:09

Arvind Kumar