Why use Rest Template

Posted By : Ganesh Chandra Tiwari | 23-Dec-2017


RestTemplate:-
The RestTemplate is the basic Spring class for simultaneous client-side HTTP access. It simplifies the interaction with HTTP servers and enforces RESTful systems. It is very related to the JdbcTemplate, JmsTemplate and the various other templates. 

Why should we use RestTemplate:-
Basically, RestTemplate is used to make HTTP Rest Calls (REST Client).
If we want to make an HTTP Call, we need to create an HttpClient, pass request and form parameters, setup accept headers and perform unmarshalling of response, all by yourself, Spring Rest Templates tries to take the pain away by abstracting all these details from you.
It is thread-safe, once created you can use it as a callbacks to customize its works.


The main entry points of the template are the six methods.

1. HEAD:-
   HTTP HEAD method is used to get all headers information.

    public HttpHeaders headForHeaders(String, String)

2. Exchange : 
    It executes the HTTP method for the given URI. It returns ResponseEntity object and can interact using any HTTP method.

    pubilc ResponseEntity<String> exchange(URI, HttpMethod methodType, HttpEntity<String> entity, String.class)

3. GET:-    
    For the given URL, HTTP GET method is used to get as an entity.

    public Object getForObject(String, Class, String)

4. POST:-
    HTTP POST method is used to creates a new resource and returns the position of the created new resource.

    public URI postForLocation(String, Object, String)

5. DELETE:-
    HTTP DELETE method is used to delete the resources of the given URL.

    public void delete(String, String)

6. PUT:-
    HTTP PUT method is used to create new resource or update of the given URL.

    public void put(String, Object, String)


These all methods are used to call HTTP request. And Methods contains "Method Name" and "what this method will return".
Eg. getForObject() performs a GET call, converts the HTTP response into an object and returns that object. 
Eg. postForLocation() is a POST call which converts the given object to HTTP  request and returns HTTP Location as response which can be seen in the object.


We can configure RestTemplate to time out:-

RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
 
private ClientHttpRequestFactory getClientHttpRequestFactory() {
    int timeout = 5000;
    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory
      = new HttpComponentsClientHttpRequestFactory();
    clientHttpRequestFactory.setConnectTimeout(timeout);
    return clientHttpRequestFactory;
}

 

URI Templates:-

Every methods of URI takes as initial arguments. That can be a URI template and variables can be used to enlarge the template to a normal URI. The template variables are passed in two forms
1.  String variable arguments array,
2.  Map<String, String>. 
The string variable-arguments alternatively expand the given template variables.

Eg.
String result = restTemplate.getForObject("http://example.com/car/{car}/bookings/{booking}", String.class, "4355", "21");


it will perform a GET call on http://example.com/car/4355/bookings/21. The map we used expands the template based on variable name and is more useful when using many variables, or when a single variable is used multiple times. For example:


Map<String, String> map = new HashMap<String, String>();
map.put("car", "4355");
map.put("booking", "21");
String result = restTemplate.getForObject("http://example.com/car/{car}/bookings/{booking}", String.class, map);


Thanks, I hope it will help you.

About Author

Author Image
Ganesh Chandra Tiwari

Ganesh is a Web Application Developer. He has knowledge of Spring, Struts, Hibernate, Angular JS, HTML, CSS, Javascript.

Request for Proposal

Name is required

Comment is required

Sending message..