View RSS Feed

Spring Framework

RESTful Controllers in Spring

Rate this Entry
by , 11-28-2011 at 01:47 AM (2886 Views)
In my previous tip, I presented to you an overview of how to handle RESTful URLs. In this tip, I will followup will look at RESTful Controllers. From my previous posts, I believe you have surmised that Spring MVC’s model is a very flexible framework for for writing controller classes. Almost any method with almost any signature can be annotated to handle a web request. Although this is nice for writing RESTless controllers, it is not ideal for writing RESTful controllers. The reason is that controllers made in this way are action oriented and not resource oriented. This can be easily surmised by the names used for the different requests which are focused on the particular use cases but not on the resources.

In the previous post, I presented some RESTFUL URLs whose path is parameterized. Now in order to effectively use this, it is necessary to have a controller whose handler method can take its input from the URL’s path. In order to enable parameterized URL paths, @PathVariable annotation was introduced as part of Spring 3. Below I have written a new CarController, that takes a resource-oriented approach to handling requests for Cars.

Java Code:
package com.acme.springwebapp.web;

import javax.inject.Inject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; 
import com.acme.springwebapp.service.CarService;

@Controller
@RequestMapping("/cars")
public class CarController {
	private CarService carService;

	@Inject
	public CarController(CarService carService) {
		this.carService = carService;
	}

	@RequestMapping(value = "/{id}", 
			method = RequestMethod.GET)
	public String getCar(@PathVariable("id") long id, Model model) {
		model.addAttribute(carService.getCar(id));
		return "cars/view";
	}
	
	@RequestMapping(value="/cars", 
			method=RequestMethod.GET) 
	public String listCars(@RequestParam("num") long num, Model model) { 
		model.addAttribute(carService.getCars((int)num));
		return "cars/list"; 
	}
}
Note that the CarController has been annotated with @RequestMapping at the class level to indicate that this controller will handle requests for Car resources—requests whose URLs start with /cars.
I’ve only created a handler method—the getCar() method. When this method’s @RequestMapping annotation is coupled with the class-level @Request- Mapping, this method is set to handle GET requests for URLs that take the form /cars/{id}. The curly braces part that says {id} is a placeholder through which variable data will be passed into the method. It corresponds to the @PathVariable annotation on the id method parameter. If you choose to not use the phrase id for the URL path placeholder, the value of @PathVariable annotation and as the actual name of the method parameter. You could leave it out as the @PathVariable’s value.

Java Code:
@RequestMapping(value="/{id}", method=RequestMethod.GET) 
public String getSpittle(@PathVariable long id, Model model) {
	model.addAttribute(spitterService.getSpittleById(id));
	return "spittles/view";
}
With no @PathVariable value, the method parameter name serves at the path variable name. That’s it for now. Next we will look at some of the key verbs used in REST.

Submit "RESTful Controllers in Spring" to Facebook Submit "RESTful Controllers in Spring" to Digg Submit "RESTful Controllers in Spring" to del.icio.us Submit "RESTful Controllers in Spring" to StumbleUpon Submit "RESTful Controllers in Spring" to Google

Categories
MVC , Controller , REST , Spring 3

Comments