Monday, December 07, 2009

Spring and REST

After loving for more than two years Don Brown's fantastic REST plugin for Struts2 I decendent again into Spring's version of REST (see My simple porting of Don Brown's demo app to Spring failed again (I tried 12 months ago to use some work from carbon five) Anyway why, why does it not work I thought I had it figured out:

package edu.ucsd.extension.springmvchelloworld;

import java.util.Collection;

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.servlet.ModelAndView;

public class OrdersController {

private Order model = new Order();
private Collection list;
private OrdersService ordersService = new OrdersService();

// GET /orders/1
public String show(@PathVariable String id, Model model) {
System.out.println("Showing order " + id);
model.addAttribute("order", getModel());
return "show";

// GET /orders
public String index(Model model) {
list = ordersService.getAll();
model.addAttribute("orders", list);
return "orders-index";

// GET /orders/1/edit
public String edit(@PathVariable String id, Model model) {
model.addAttribute("order", getModel());
return "orders-edit";

// GET /orders/new
public String editNew(Model model) {
model.addAttribute("order", new Order());
return "orders-editNew";

// GET /orders/1/deleteConfirm
public String deleteConfirm(@PathVariable String id) {
return "orders-deleteConfirm";

// DELETE /orders/1
@RequestMapping(value="/{orderId}", method=RequestMethod.DELETE)
public String destroy(@PathVariable String id) {
//addActionMessage("Order removed successfully");
return "orders-index";

// POST /orders
public ModelAndView create(Order model) {;
return new ModelAndView("orders-show", "model", getModel());

// PUT /orders/1
@RequestMapping(value="/{orderId}", method=RequestMethod.PUT)
public String update(@PathVariable String id, Order model) {;
return "order-index";

public void setId(String id) {
if (id != null) {
this.model = ordersService.get(id);

public Object getModel() {
return (list != null ? list : model);


Well, I guess there is some other trick I am missing.

BTW: the repository in the POM file needs to look like:

Springframework milestone


Post a Comment

<< Home