What Is Spring RestTemplate?

The Spring RestTemplate is a class in spring designed to simplify the use of Rest APIs during development for client and server. Similar to other templates such as the Spring JDBCTemplate , they make it much easier to develop code to process Rest APIs by hiding much of the lower level complexity involved in processing the HTTP access required on the client side when accessing HTTP servers for Rest. The RestTemplate was added to Spring in version 3.0

What HTTP Methods Does Spring RestTemplate Work For?

The RestTemplate in spring lines up with the 6 main HTTP methods that are available

DELETE

The delete() methods delete the resources at the specified URI

GET

The get() methods retrieve an object or entity by doing a GET at the specified URI. The response is converted and returned. The Spring Web Service Client Example shows the basics of how to use get to retrieve objects from a HTTP Server

HEAD

The headForHeaders() methods will retrieve the headers of the resource specified by the URI.

OPTIONS

The optionsForAllow() methods will return the value of the Allow header for the given URI. This will indicate which HTTP methods are allowed for the specified URI.

POST

The postForLocation(), postForEntity() and postForObject methods will post to the specified URI, and return the location, entity or object response if any. The Spring Web Service Client Example shows the basics of how to use post to a HTTP Server

PUT

The PUT method will put an object to the specified location. The return type for the method is void so there is no response expected.

Any HTTP Method

The execute and exchange methods allow the execution of a specified HTTP method to the given URI , writing the request, and returning the response. this allows for a bit more flexibility in how the HTTP methods are processed.

How Do You Convert The Messages From The HTTP Server?

The RestTemplate uses HttpMessageConverters to handle the conversion of the data passed to and received from the HTTP server. There are HttpMessageConverters included with the RestTemplate for handling the typical mime message types to be processed with Rest APIS, but you do have the option of course of creating your own to handle more complete messages and flows.

Resources

Spring RestTemplate Javadoc

DZone Spring RestTemplate

Spring Consuming Rest

Leave a Comment

Your email address will not be published.