SpringMVC 構建Restful風格 及問題處理
阿新 • • 發佈:2018-11-21
轉自:http://www.cnblogs.com/loveincode/p/7520340.html
章節目錄
基本的請求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/