Summary: RESTful Web Services with Spring

BTI360 Blog / May 3, 2010 in 

The Spring Framework is the current dominant JEE framework.  Spring allows you to expose legacy web applications as Web Services without changing much code.  This post will describe what you need to create a RESTful Web Service in Spring.  This information reinforces the topics introduced in the “Building RESTful Web Services with Spring” screencast.

First, add two tags to the servlet configuration file:

<context-component-scan base-package='com.bti'/>

This tag enables auto-detection of annotated components, such as @Controller and @Repository, in the package specified by the base-package attribute.  Then the following tag:

<mvc-annotation-driven/>

Using this tag is the best way of jump starting a Spring MVC project.  This tag does all of the following:

  • Registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans which are required for Spring to dispatch requests to @Controller annotated classes.
  • Registers those beans to use JAXB to read and write XML and Jackson to read and write JSON if JAXB and Jackson are in your classpath.
  • Registers other converters to convert different number formats and date/time formats.

Spring implements RESTful Web Services by mapping HTTP requests and methods to Controller methods using custom annotations.  Here’s a look at these annotations:

  • @Controller – Let’s Spring know that this class will serve as a controller in the MVC pattern. Spring will search these classes for methods annotated with @RequestMapping.
  • @RequestMapping – Used to map URLs onto an entire class or method. If the annotation is used at the class level @RequestParams can be used to narrow the mappings. Mappings can also be narrowed by the request method (GET/POST/DELETE/PUT)
  • @PathVariable – Used to map variables in the URI in the @RequestMapping to method parameters

In addition, there are many other types of advanced mappings for the @RequestMapping annotation:

  • @RequestBody – Used to map the request body onto a variable.  Can also map to Beans such as our Word object, using either JAXB or Jackson to convert the data from either xml or json.  Uses the content type to determine the format.
  • @ResponseBody – Used to let Spring know that you want whatever you are returning from the method to be written to the response, and the response status will be OK by default.  The output format depends on the Accepts header of the request.
  • @ResponseStatus – Lets Spring know what status to write to the response.
  • @ExceptionHandler – Tells Spring that a type of exception thrown in a controller should be handled by the annotated method. Used along with @ResponseStatus, this annotation also gives the ability to have a request return different statuses based on the outcome of the request.
  • @RequestParam – Maps a request or query parameters to method parameters.

This is just a brief overview of building RESTful Web Services with Spring.  If would like more information please check out the “Building RESTful Web Services with Spring” screencast series. The source code used in the screencast is available at http://bti360.googlecode.com, under screencasts/SpringDictionaryService.

Previous

RESTful Web Services with Spring – Part 3

Next

Introduction to RESTful Clients

Close Form

Enjoy our Blog?

Then stay up-to-date with our latest posts delivered right to your inbox.

  • This field is for validation purposes and should be left unchanged.

Or catch us on social media

Stay in Touch

Whether we’re honing our craft, hanging out with our team, or volunteering in the community, we invite you to keep tabs on us.

  • This field is for validation purposes and should be left unchanged.