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 http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/) 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;
@Controller
public class OrdersController {
private Order model = new Order();
private Collectionlist;
private OrdersService ordersService = new OrdersService();
// GET /orders/1
@RequestMapping("{orderId}")
public String show(@PathVariable String id, Model model) {
System.out.println("Showing order " + id);
setId(id);
model.addAttribute("order", getModel());
return "show";
}
// GET /orders
@RequestMapping
public String index(Model model) {
list = ordersService.getAll();
model.addAttribute("orders", list);
return "orders-index";
}
// GET /orders/1/edit
@RequestMapping("/{orderId}/edit")
public String edit(@PathVariable String id, Model model) {
this.setId(id);
model.addAttribute("order", getModel());
return "orders-edit";
}
// GET /orders/new
@RequestMapping("/new")
public String editNew(Model model) {
model.addAttribute("order", new Order());
return "orders-editNew";
}
// GET /orders/1/deleteConfirm
@RequestMapping("/{orderId}/deleteConfirm")
public String deleteConfirm(@PathVariable String id) {
this.setId(id);
return "orders-deleteConfirm";
}
// DELETE /orders/1
@RequestMapping(value="/{orderId}", method=RequestMethod.DELETE)
public String destroy(@PathVariable String id) {
ordersService.remove(id);
//addActionMessage("Order removed successfully");
return "orders-index";
}
// POST /orders
@RequestMapping(method=RequestMethod.POST)
public ModelAndView create(Order model) {
ordersService.save(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) {
ordersService.save(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:
org.springsource.maven.snapshot
Springframework milestone
http://maven.springframework.org/milestone
0 Comments:
Post a Comment
<< Home