Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does REST (RestTemplate) in Spring Library support HTTPS protocol?

I'm trying to connect to a web server via HTTPS, but response.getBody() returns null however I want to return a JSON array. The statusCode is 200 and the headers contain the correct information, but the response body is null. I'm using the standard Spring RestTemplate API for this purpose specifically postForEntity(). Maybe in order to do this I have to use some special Spring API?

Unfortunately I couldn't find any information about HTTPS support and SSL/'TLS' certificates in the Spring REST documentation (it is quite limited).

like image 898
Oleksandr Karaberov Avatar asked Dec 19 '12 15:12

Oleksandr Karaberov


People also ask

What HTTP client does RestTemplate use?

The HTTP client library takes care of all the low-level details of communication over HTTP while the RestTemplate adds the capability of transforming the request and response in JSON or XML to Java objects. By default, RestTemplate uses the class java. net. HttpURLConnection as the HTTP client.

Is Spring rest template thread safe?

Conceptually, it is very similar to the JdbcTemplate, JmsTemplate, and the various other templates found in the Spring Framework and other portfolio projects. This means, for instance, that the RestTemplate is thread-safe once constructed, and that you can use callbacks to customize its operations.

Is RestTemplate secure?

It automatically marshals/unmarshals the HTTP request and response bodies. Using RestTemplate is thread safe.


1 Answers

You can configure the RestTemplate with the HttpComponentsClientHttpRequestFactory, for example:

<bean id="httpComponentsClientHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"/>

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <constructor-arg ref="httpComponentsClientHttpRequestFactory"/>
</bean>

That allows the RestTemplate to use the Apache HttpComponents HttpClient under the hood, which definitely supports SSL.

It looks like the HttpClient provided by HttpComponentsClientHttpRequestFactory supports SSL out of the box, so there may be almost no configuration required on your side.

like image 181
Jonathan Avatar answered Oct 03 '22 07:10

Jonathan