Search code examples
web-servicesspringhttpclientresttemplate

Simplest way to make HTTP request in Spring


In my Spring web application I need to make an HTTP request to a non-RESTful API, and parse the response body back as a String (it's a single-dimension CSV list).

I've used RestTemplate before, but this isn't RESTful and doesn't map nicely on to classes. Whenever I implement something like this 'by hand' (eg using HttpClient) I invariably find out later that Spring has a utility class that makes things much simpler.

Is there anything in Spring that will do this job 'out of the box'?


Solution

  • If you look at the source of RestTemplate you will find that internally it uses

    java.net.URL
    

    and

    url.openConnection()
    

    that is the standard way in Java to make HTTP calls, so you are safe to use that. If there would be a "HTTP client" utility in spring then the RestTemplate would use that too.