11

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).

mtotowamkwe
  • 2,407
  • 2
  • 12
  • 19
Oleksandr Karaberov
  • 12,573
  • 10
  • 43
  • 70
  • What does "can't get any response" exactly mean? Any error code? – Adrian Dec 19 '12 at 15:30
  • Okay, and what's the status code of the response? If it is "200" the request itself should be okay. Can you verify on the server side that the request how you send it generally works? For instance try to call the url your application calls with your webbrowser. – Adrian Dec 19 '12 at 15:37

1 Answers1

19

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.

Ian
  • 50,146
  • 13
  • 101
  • 111
Jonathan
  • 20,053
  • 6
  • 63
  • 70
  • @AlexanderKaraberov You can add it to the Application Context or you can create the objects programatically, e.g. new RestTemplate(...) I just used an xml example as that's generally typical. Unfortunately I cannot comment on Android, I don't have much experience there. :-) – Jonathan Dec 19 '12 at 15:43
  • @AlexanderKaraberov I wasn't aware of this change, I've updated my answer. Hope that helps. – Jonathan Dec 19 '12 at 16:02
  • @AlexanderKaraberov No problem, glad I could help in some way. – Jonathan Dec 19 '12 at 16:22
  • @Jonathan, how can i implement this factory through **new RestTemplate(...)**? What should i put inside *RestTemplate()*? – Alexandre May 10 '13 at 18:07
  • 2
    @Alexandre The equivalent in Java would be: `new RestTemplate(new HttpComponentsClientHttpRequestFactory())`. If you need further assistance, I recommend you ask it as new question - you'd at least get more timely responses. :-) – Jonathan May 13 '13 at 08:04
  • I'm aware I'm commenting on an older post here, but I think you may need to update the constructor-arg ref to use the changed bean name. Otherwise +1; it helped me! :) – Ben Siver Jul 30 '13 at 14:19
  • Also, you must add httpclient to your dependencies, if you are using maven, as this is not automatic in any way... – P.Péter Apr 09 '15 at 16:26