Monday, August 26, 2013

Restful web services using Spring 3 MVC Part 2



In my last post(Restful web services using Spring 3 MVC), I described how to write Spring rest web services for HTTP GET method. I am using RestTemplate to invoke HTTP GET method of web-service in this post.
RestTemplate is the central Spring class for client-side HTTP access. Conceptually, it is very similar to the JdbcTemplate, JmsTemplate, and the various other templates found in the Spring Framework. The RestTemplate is thread-safe once constructed, and that you can use callbacks to customize its operations. RestTemplate Methods The main entry points of the template are named after the six main HTTP methods:
HTTP RestTemplate
DELETE delete(String, String...)
GET getForObject(String, Class, String...)
HEAD headForHeaders(String, String...)
OPTIONS optionsForAllow(String, String...)
POST postForLocation(String, Object, String...)
PUT put(String, Object, String...)

For RestTemplate instance, getForObject() will perform a GET, convert the HTTP response into an object type of your choice, and returns that object.
To test getForObject() method deployed employeeMgt.war on tomcat, and run below test method.
Another way to call HTTP GET method is by exchange() method of RestTemplate.
To use exchange method, first create list of accepted media types.(Line no. 9)
After that prepare HttpHeaders, and set List(Line No. 13)
Prepare HttpEntity, that use in exchange method of restTemplate.(Line No. 17)
Instance of ResponseEntity is returned from restTemplate.exchange().(Line No. 22)

No comments:

Post a Comment