October 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Spring 3.0 Support for RESTful Web Services

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

Although RESTful functionality was added to the Spring MVC framework quite early through annotations and other API features, support for RESTful Web services came a bit late to Spring MVC. Several JAX-RS (Java API for RESTful Web services) implementations such as Restlet, RESTEasy and Jersey supported RESTful Web services already, but the Spring community didn't add support for RESTful Web services features until Spring 3.0. In this article, I discuss the Spring 3.0 support for developing RESTful Web services, examining the classes and annotations provided.

As a quick refresher, RESTful Web services are Web services that are created and accessed using REST (REpresentational State Transfer) principles. RESTful Web services, which use HTTP methods for the operations they perform, can be easily accessed using Uniform Resource Identifiers (URIs). The HTTP methods POST, GET, PUT and DELETE can be mapped to create, read, update and delete (CRUD) operations.

RESTful Web Services Using JAX-RS

JAX-RS is a Java programming API (JSR 311) that is included as part of the Java EE platform. JAX-RS is designed to make it easy to develop Java applications using REST principles and architecture; Using annotations, JAX-RS developers can expose simple POJOs as Web resources. JAX-RS applications are bundled as WAR files like any other Web application in Java and deployed on a container that supports the Java Servlet API.

One way to develop JAX-RS applications in Java is to use Jersey, an open source, production-quality reference implementation of JAX-RS. Jersey implements all the APIs, and it provides annotations for quick and easy creation of Java Web services using the REST style. It also provides a lot of additional features with its own APIs like the Jersey Client API. For RESTful Web services, Jersey uses a HTTP Web server called Grizzly. Requests to this container are handled by the Grizzly Servlet, with the fully qualified name com.sun.jersey.spi.container.servlet.ServletContainer.

RESTful Web Services Using Spring

With Spring MVC support for RESTful Web Services, Java developers can build RESTful applications using annotations that inherit from the annotations in Spring's Web MVC framework. Client-side support for RESTful applications are provided by the RestTemplate API, which is conceptually very similar to classes like JdbcTemplate and JmsTemplate. REST functionality is provided by the HttpConverters class, which helps convert objects to their representations in HTTP requests and responses, and vice versa. To achieve the mapping between objects and XML, Spring provides the MarshallingHttpMessageConverter class.

Spring Web MVC uses a DispatcherServlet for dispatching requests to handlers. The default handler is identified using the @Controller and @RequestMapping annotations. These annotations play a major role in developing RESTful applications for Spring, and in fact are fundamental (along with the @PathVariable annotation) to the RESTful functionality in Spring. The @Controller annotation is used for marking a POJO as a Controller, while the @RequestMapping annotation is used for mapping requests to a class or a handler method.

Spring Controllers and RESTful Web Services

Controllers represent the "C" in Spring Web MVC. They help handle user input and transform it into a model for the user through the view. Controllers can be declared using the @Controller annotation. Typically, this annotation is used on top of a class to mark that particular class as a controller.

@Controller
public class StockController {
private final Stock stock;

@AutoWired
public StockController(Stock stock) {
this.stock = stock;
}

@RequestMapping("/")
public void stockInfo() {
}

@RequestMapping("/stockdetails")
public String getStockDetails() {
return this.stock.getStockDetails();
}
}

The annotation @RequestMapping is not required at the class level, it can be mentioned at the method level. The appropriate method is invoked by the handler depending on the absolute path.

URI Templates

A URI template is a template to hold a URI like string. Typically, a URI template holds variables and when these variables are substituted with values, they become the actual URI. URI templates allow mentioning a template in the @RequestMapping annotation.

For example, the URI http://www.helloworld.com/users/{username} mentions a URI template that allows one to substitute the username with the actual name of a user like http://www.helloworld.com/users/john. The handler compares the actual URI with the URI template and substitutes the parameters appropriately.

@RequestMapping(value="/users/{username}", method=RequestMethod.GET)
public String sayHello(Model model) {
......
return "Hello, Welcome!";
}

Spring Annotation: @PathVariable

The annotation @PathVariable is used to bind the URI template variable to a method parameter. In the previous example, the URI template variable username can be bound to the method parameter userName with the @PathVariable annotation. The method parameter types declared with @PathVariable are not restricted only to String; they can be of any primitive data type such as int, long, double, etc.

@RequestMapping(value="/users/{username}", method=RequestMethod.GET)
public String sayHello(@PathVariable("username") String userName, Model model) {
......
return "Hello "+userName+", Welcome!";
}

It is possible to bind multiple URI template variables to the method parameters also.

@RequestMapping(value="/users/{username}/city/{cityname}", method=RequestMethod.GET)
public String sayHello(@PathVariable("username") String userName, @PathVariable("cityname") String cityName, Model model) {
......
}

URI template variables can come from the relative path as well.

@Controller
@RequestMapping(value="/users/{username}")
public class SayHelloController {

@RequestMapping(value="/city/{cityname}")
public String sayHello(@PathVariable("username") String userName, @PathVariable("cityname") String cityName, Model model) {
......
}
}

Parameterized conditions like myParam=paramValue are also supported, wherein the request is mapped only if the parameters have the given paramValue.

Spring Annotation: @RequestParam

The annotation @RequestParam is used to bind the request parameters to a method parameter in the controller. Similarly, the annotation @RequestBody is used to indicate that the parameter is bound to the value in the HTTP request body. While sending the response, the annotation @ResponseBody can also be used to return the value in the HTTP response body.

@Controller
@RequestMapping("/stockquote")
public class StockController {
.....

@RequestMapping(method=RequestMethod.GET)
public double getStockPrice(@RequestParam("stockSymbol") String stockSymbol, Model model) {
...
model.setAttribute("stockSymbol", stockSymbol);
...
}
.....
.....
}

Tags: Web services, Spring, REST

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

Page 1 of 2



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel