SpringMVC 构建Restful风格 及问题处理
基本的请求URL:
/person/{id} GET 得到id的person
/person POST 新增person
/person/{id} PUT 更新id的person
/person/{id} DELETE 删除id的person
源码地址:https://github.com/loveincode/ssm
1. 查询 GET
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
1 @RequestMapping(value = "/{id}", method = RequestMethod.GET) 2 public @ResponseBody String show(@PathVariable Integer id, HttpServletRequest request, 3 HttpServletResponse response) { 4 log.info("ENTER " + ToolsUtil.getMethodName()); 5 ResultVO resultVO = new ResultVO(); 6 Person person = new Person(); 7 person = personService.findById(id); 8 if (person != null) { 9 resultVO.setSuccess(true); 10 resultVO.setData(person); 11 resultVO.setMessage("查询成功"); 12 } else { 13 resultVO.setMessage("查询失败,没找到id=" + id + "的Person"); 14 } 15 return resultVO.toString(); 16 }
测试:
2. 新增 POST
@RequestMapping(method = RequestMethod.POST)
1 @RequestMapping(method = RequestMethod.POST) 2 public @ResponseBody String add(@ModelAttribute("person") Person person, HttpServletRequest request, 3 HttpServletResponse response) { 4 ResultVO resultVO = new ResultVO(); 5 System.out.println(person.toString()); 6 if (person.getName() != null) { 7 personService.add(person); 8 resultVO.setSuccess(true); 9 resultVO.setMessage("插入成功"); 10 } else { 11 resultVO.setMessage("name为空,插入失败"); 12 } 13 return resultVO.toString(); 14 }
测试:
3. 更新 PUT
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
@RequestMapping(value = "/{id}", method = RequestMethod.PUT) @ResponseBody public String update(@PathVariable Integer id, @ModelAttribute("person") Person person, HttpServletRequest request, HttpServletResponse response) { ResultVO resultVO = new ResultVO(); Person oldperson = personService.findById(id); if (oldperson != null) { if (person.getName() != null) { person.setId(oldperson.getId()); personService.update(person); resultVO.setMessage("更新成功"); } else { resultVO.setMessage("更新失败,name为空"); } } else { resultVO.setMessage("更新失败,不存在 id = " + id + "的Person"); } return resultVO.toString(); }
测试:
4. 删除 DELETE
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
1 @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) 2 @ResponseBody 3 public String delete(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) { 4 ResultVO resultVO = new ResultVO(); 5 Person person = new Person(); 6 person = personService.findById(id); 7 if (person != null) { 8 resultVO.setSuccess(true); 9 personService.delete(id); 10 resultVO.setMessage("删除成功"); 11 } else { 12 resultVO.setMessage("删除失败,不存在id = " + id + "的Person"); 13 } 14 return resultVO.toString(); 15 }
测试:
5. 问题
5.1 SpringMVC接收不了PUT、DELETE bodydata 解决方法
需要在web.xml中加上:
<filter> <filter-name>HttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
然后在请求PUT、DELETE的时候,使用POST请求,在body中加入_method 参数,参数为PUT或DELETE,即可完成对PUT、DELETE的处理。
例如:
strtus2 构建restful风格 https://www.ibm.com/developerworks/cn/java/j-lo-struts2rest/