Saturday, April 20th, 2024

Restful URLs In Spring 3

1

Introduction

With the release of the Spring Framework 3.0, restful urls are now fully supported through a few simple annotations. This post will show you how to easily add restful urls to your spring project and assumes you already have an understanding of Spring MVC. The code snippets in this post refer to an example CD library application that stores metadata about audio CDs in a database.

Application Context

There are a couple of things you need to add to your application context to enable annotations for restful urls. The first thing to do is to add an element to the application context of your spring web application that tells Spring to scan for components/classes with annotations. The following is the code snippet to add to your application context configuration:


Notice that the ‘base-package’ defines where Spring should start scanning for annotated controllers and classes.

The second thing you may need to add to your application context configuration is the default annotation handler mapping class which tells Spring to map requests to annotated controllers. The code snippet below shows how this handler mapping is added:


The annotation handler mapping configuration only needs to be added if you have other custom mappings in your application context configuration, if you don’t have other custom mappings in your application context configuration then Spring ‘auto-magically’ adds the handler mapping for you.

Controller Annotations

Now you need to configure your controller class through annotations. There are a couple of annotations that need to be added at your class level as follows:

	@RequestMapping("/cd")
	@Controller
	public class CdRecordController
	{...

The ‘@Controller’ annotation tells Spring that this class is a controller and will then scan the class for request mappings. The ‘@RequestMapping’ then tells Spring the pattern of requests to map to this controller, in our case we are mapping all requests starting with ‘/cd’ to this controller.

Method Annotations

Mapping specific requests to methods is easy with the ‘@RequestMapping’ annotation. The example below will map requests for a specific CD to the method:

	@RequestMapping(value="/view/{cdId}", method=RequestMethod.GET)
	public ModelAndView getSpecificCd(@PathVariable("cdId") String cdId)
	{...

The CD identifier is part of the url and is passed to the method as a parameter using the ‘@PathVariable’ annotation.

The following example maps a request onto a method to show all CDs in the library/collection:

	@RequestMapping(value="/view/", method=RequestMethod.GET)
	public ModelAndView listAllCds()
	{...

As a general note, method mappings are only allowed to narrow any request mapping at the class level. So in the above examples all request mappings for the methods would narrow the class mapping of ‘/cd’ further.

Mapping Syntax

Request mappings also support Ant path like mappings. As an example, the mapping ‘/*/printview’ would map all requests with ‘printview’ at the end of the url to the associated method. View the javadoc for org.springframework.web.bind.annotation.RequestMapping for more details on the annotation syntax and other parameters.

Conclusion

This post has given you a quick overview of how to quickly get up and running with restful urls in Spring 3. Restful urls make it easier for users of a web application to bookmark the view of a specific piece of data because the identifier for that piece of data is part of the url rather than being a parameter at the end of a url. For more information on the use of restful urls in the Spring Framework please visit the Spring website.

Share

Comments

One Response to “Restful URLs In Spring 3”
  1. SMiGL says:

    Helpful article. Thanks