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)

Sunday, August 25, 2013

Restful web services using Spring 3 MVC Part 1



According to wikipedia :
REST-style architectures conventionally consist of clients and servers. Clients initiate requests to servers; servers process requests and return appropriate responses. Requests and responses are built around the transfer of representations of resources. A resource can be essentially any coherent and meaningful concept that may be addressed. A representation of a resource is typically a document that captures the current or intended state of a resource.
The client begins sending requests when it is ready to make the transition to a new state. While one or more requests are outstanding, the client is considered to be in transition. The representation of each application state contains links that may be used the next time the client chooses to initiate a new state-transition.
In addition to URIs; Internet media types; request and response codes; etc., HTTP has a vocabulary of operations called request methods, most notably:
GET
POST
PUT
DELETE
REST uses these operations and other existing features of the HTTP protocol. For example, layered proxy and gateway components perform additional functions on the network, such as HTTP caching and security enforcement.
In this post I am discussing about GET request method.
CODING
Let’s start with a simple Spring MVC application created with Spring MVC template. In servlet-context.xml where component-scan, annotation-driven and InternalResourceViewResolver are registered.
Now create two simple POJO classes Employee and EmployeeList. EmployeeList contains one attribute List with getter and setters. Classes are converted to its XML representation using Jaxb annotation @XmlRootElement. Jaxb allows developers to map Java classes to XML representations and vice-versa.
Employee.java EmployeeList.java
Now create one class RestProviderController.java which is annoted as @Controller. This class will work as controller and call to it's respective method according to URL mapping. getEmployees() return EmployeeList an-noted as @ResponseBody. @RequestMapping used an another parameter header attributes for request and response.
RestProviderController.java
Create a war of project named employeeMgt.war and deployed on tomcat. I am running my tomcat at port 9090. Hit the URL from any rest client(I am using RestClient A firefox plugin for testing the RESTful webservices).
URL : http://localhost:9090/employeeMgt/restService/employees/
If I am returning a EmployeeList object and output is a XML, where is conversion between object and XML? A new concept: HttpMessageConverters. HttpMessageConverter is responsible for converting from HTTP request message to an object and converting from an object to HTTP response body. Next HttpMessageConverters are registered by default:
Now instead of returning XML we want to return JSON. Change could not be easier, add Jackson library in lib and change @XmlRootElement to @JsonAutoDetect. And now MappingJacksonHttpMessageConverter will handle this object and will transform Character instance to JSON protocol using Jackson library. Only changing one line of code!!!

Saturday, August 24, 2013

What made me to write this blog...


Hi Readers,

This post is going to be my first post at this blog, related to spring.  From long time I was looking to learning and writing something about Spring. While working in spring I felt that many times (or infect all the time) I use to look up Google and try to find solution for my problem, many time I get complete solution for my problem, sometime just some part of the problem and sometimes even I didn't got anything for my problem.

So I was just thinking till when I can expect this to be one sided. We should also start sharing our experience so that it can help someone like me who will be looking for the solutions. The game should be from both side, there is famous saying in India that you should not think what you have got from society what you should think is What you have contributed for society?

So with this thought I am starting to write blogs on spring for beginners. They could be very simple but it may help to someone like me who just know the A,B,C .. of spring :) . Hope what I have thought I will be able to complete.

Thanks All

Hoping for your support..

- Prafful