December 22, 2014
Hot Topics:

RESTful Clients in Spring, Page 2

  • January 3, 2011
  • By Sangeetha S
  • Send Email »
  • More Articles »

As mentioned previously, RestTemplate is the class used for accessing RESTful services on the client side. Even though this is similar to other template classes available in Spring, it can be customized further by providing callback methods and configuring the HttpMessageConverter class for marshalling and unmarshalling HTTP request and response. Client-side operations are performed completely with the RestTemplate and HttpMessageConverter classes.

The RestTemplate Class

The Jakarta Commons HttpClient class is used for invoking RESTful services in Java. For common operations, the executeMethod of the HttpClient is used for performing higher-level operations corresponding to the six HTTP methods. RestTemplate provides a set of methods that ensures REST best practices. The methods provided by the RestTemplate class that correspond to HTTP methods are:

RestTemplate class and corresponding HTTP methods

The method names in the RestTemplate class convey a simple meaning. The first part contains the name of the HTTP method to which the RestTemplate method names correspond, and the second part indicates the return value. To understand this better, consider the getForObject method: it performs a GET operation and returns an object from the HTTP response (HTTP response is converted into an object type). Similarly, the postForLocation method will perform a POST operation and return the HTTP location header where the newly created object is available.

The methods in the RestTemplate class accept a string URL or map strings. The RestTemplate object can be created using the default constructor, and the client uses the APIs in the java.net package to create HTTP requests. Here is an example of a client accessing RESTful Web services using the RestTemplate class:

........
String uri = "http://stock.com/stockdata/{symbol}/stockquote";
RestTemplate template = new RestTemplate();
StockQuote sq = new StockQuote();
........
URI location = template.postForLocation(uri, sq, "INFY");
........

HTTP Message Converters

Objects sent to and returned from the RestTemplate methods, such as getForObject, put, postForObject, etc., are converted to HTTP messages -- HTTP request and HTTP response -- by the HttpMessageConverter interface. This interface has the following methods:

  • boolean canRead(Class<?> clazz, MediaType mediaType) -- Used to determine whether the given class and media type can be read by this converter
  • boolean canWrite(Class<?> clazz, MediaType mediaType) -- Used to determine whether the given class and media type can be written by this converter
  • List<MediaType> getSupportedMediaTypes() -- Returns the list of MediaType objects supported
  • T read(Class<T> clazz, HttpInputMessage inputMessage) -- Reads an object from the given input message, and returns it
  • void write(T t, HttpOutputMessage outputMessage) -- Writes a given object to the given output message

The converters for the MIME type are available by default. It is possible to write custom converters also. The default converter instances available are highlighted in the table below.

Default HTTP message converter instances

Conclusion

RESTful Web services are best suited for when there is a need for stateless Web services. The REST-based architecture provides better performance and caching options in those instances. As I've discussed in this article, the Spring Framework provides good support for building RESTful Web services in a simple and elegant way with annotations and APIs like RestTemplate.

Acknowledgements

The author would like to sincerely thank Mr. Subrahmanya (SV, VP, ECOM Research Group, E&R) for his ideas, guidance, support and constant encouragement and Mr. KL Nitin for kindly reviewing this article and providing valuable comments.

Further Reading

For more information on REST and REST principles, read these articles from DevX:

About the Author

Sangeetha S. works as a Senior Technical Architect at the E-Commerce Research Labs at Infosys Technologies. She has over 12 years of experience in the design and development of Java and Java EE applications. She has co-authored a book on J2EE architecture and also has written articles for online Java publications.


Tags: Web services, Spring, REST

Originally published on http://www.developer.com.

Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel