Calling REST service with Spring RestTemplate

In this screencast we will walk through how to call a RESTFul webservice in java with spring's RESTTemplate.

Detailed Video Notes

Getting started

As REST services continue to gain popularity you will be faced with question on whether or not you should make server side HTTP request or develop a native client. In this screen cast we will show how to make a RESTFul services request with spring's RESTTemplate.

What is a RestTemplate


RestTemplate is a class used to make HTTP Rest calls. It is similar to JdbcTemplate and JmsTemplate where these abstractions encapsulate lower level calls what can be performed with a library like apache http components.

Project set up


We will set up a project with spring boot and utilize the test class created.

Making a first request


SparkFun is a neat product that allows people to push data to a public free location for consumption. So if you are looking for data or want the ability to store data via REST calls, this might be an option. We found a data stream called Wimp Weather Station that provides weather readings from on top of somebody's roof in Colorado. Let's make a request to the URI and view the response as a String.

Creating a new class RestTemplate we will call getForEntity passing in a URL and the class we would like the response to be returned as.

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.getForEntity(



For sake of space, section of the nodes were omitted.

        "long":"Boulder, CO, United States",
        "country":"United States",
      "title":"Wimp Weather Station",
      "description":"Weather station on top my roof. Read the full tutorial here:",

Converting request to java object


We specified a JSON string but that doesn't do us much good, we want to convert the JSON string to a java object.

Create wrapper objects

What we need to do is unwrap the json into multiple java objects which is tedious. We will create a condensed view of objects and fields for the request by first creating a SparkResponseWrapper, SparkStreamWrapper, DocWrapper and WimpWeatherLocationWrapper. You will notice in each wrapper object we included @JsonIgnoreProperties(ignoreUnknown = true) which is an indicator to jackson to fail if an unknown property is found in the deserialization process.

Update the Entity Class Type

We next replace String.class with SparkResponseWrapper.class where jackson will know to walk the json tree populating the java objects.

public void convert_json_response_to_java_obj() {

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<SparkResponseWrapper> response = restTemplate



WimpWeatherLocationWrapper [longValue=Boulder, CO, United States, city=Boulder, state=Colorado, country=United States, lat=40.0149856, lng=-105.27054559999999]

Handling an exception


In the event that your service call fails or the marshaling of the json to the java object throws an exception you may want to log the error or retry the request. If you aren't familiar, the http response deliver a status codes which indicate whether a specific request has been successfully completed. Luckily ResponseEntity adds the status code just for this reason.

We will reference a status code enum constant and check the status code returned in the response.

public void test_successful_response() {

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> response = restTemplate.getForEntity(
    if (HttpStatus.OK == response.getStatusCode()) {
    } else {
        // log error, retry or ? 

RestTemplate with proxy

If you receive a connection timed out exception, like the one below, it is quite possible that you are sitting behind a proxy. If so, you will need to take additional steps to configure RestTemplate by using a SimpleClientHttpRequestFactory, an implementation of ClientHttpRequestFactory, and passing in a Proxy object. The snippet below should give you a reference.

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "":Connection timed out: connect; nested exception is Connection timed out: connect
Caused by: Connection timed out: connect
    at Method)
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("", 9999));

RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
ResponseEntity<String> response = restTemplate.getForEntity(

Be aware


This screen cast is a high level simple configuration and just scratches the surface on how to make a http rest request in java. Other items to investigate is making calls with request methods (GET, POST, PUT, etc), configuring a proxy, dealing with SSL certificates, connection pooling, configuring timeouts on the request and the list goes on. As you are making this tutorial and implementing a solution be aware that this is just the tip of the iceberg so be sure to reach out to your web infrastructure teams.

Hope you enjoyed today's level up, have a great day!