2011-11-10 50 views
0

我這是在春季已經實現了MVC我需要使用spring 3來實現Rest web服務嗎?

我需要用彈簧3

使用REST Web服務的同一應用程序庫的應用程序我有一個控制器類我想要的是一個RESTful Web服務

@Controller @SessionAttributes("category") 
public class CategoryController { 

private static final Log log = LogFactory.getLog(CategoryController.class); 

@Autowired 
private CategoryService categoryService; 

@Autowired 
private ItemService itemService; 

@RequestMapping("/category/categoryList.htm") 
public ModelAndView list(HttpServletRequest request, 
     HttpServletResponse response) throws Exception { 
    List<Category> list = categoryService.getAllMainCategories(); 
    Map map = new HashMap(); 
    map.put("categoryList", list); 
    map.put("category", new Category()); 
    return new ModelAndView("categoryList", map); 
} 

@RequestMapping(method = RequestMethod.POST, value = "/category/save.htm") 
public String save(HttpServletRequest request, 
     HttpServletResponse response, Category command) throws Exception { 
    log.debug("save method called" + command); 
    Category category = (Category) command; 
    System.out.println(category); 
    categoryService.saveCategory(category); 
    return "redirect:/category/categoryList.htm"; 
} 

@RequestMapping("/category/edit.htm") 
public String edit(@RequestParam String id, ModelMap model) 
     throws Exception { 
    log.debug("edit method called :" + id); 
    log.debug(Long.parseLong(id)); 
    Category cat = categoryService.getCategory(Long.parseLong(id)); 
    model.put("categoryList", categoryService.getAllMainCategories()); 
    model.put("category", cat); 
    return "categoryList"; 
} 

@RequestMapping("/category/delete.htm") 
public String remove(@RequestParam String id, ModelMap model) 
     throws Exception { 
    log.debug("remove method called " + id); 
    categoryService.deleteCategory(Long.parseLong(id)); 
    return "redirect:/category/categoryList.htm"; 
} 

@InitBinder 
protected void initBinder(WebDataBinder binder) { 
    binder.registerCustomEditor(Category.class, 
      new PropertyEditorSupport() { 
       @Override 
       public void setAsText(String text) { 
        setValue(categoryService.getCategory(Long.valueOf(text))); 
       } 
      }); 
} 

}

是CategoryController類添加刪除或更新類別

ItemService和CategoryService是數據源

類別是,如ID,名稱,描述等性質的域對象,

我如何寫這樣的REST Web服務.. ???

回答

相關問題