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).
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.
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.
It automatically marshals/unmarshals the HTTP request and response bodies. Using RestTemplate is thread safe.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With