restful的增删改查:
/** * 跳转至详情编辑页面 * @param model * @param id * @return */ @RequestMapping({"/toEditNewsDetails/{id}","/toEditNewsDetails"}) public String toEditNewsDetails(Model model, @PathVariable(value = "id",required = false) String id){ LOGGER.info("传递过来的id:"+id); News news = newsMapper.selectByPrimaryKey(!StringUtils.isEmpty(id) ? Integer.parseInt(id) : -1); model.addAttribute("news",news); return "editDetails"; } /** * 新增用户 * * @param news * @return */ @RequestMapping(method = RequestMethod.POST) // @ResponseBody public String addUser(News news) { // Map<String,Object> res = new HashMap<String, Object>(); try { LOGGER.info("进入方法addUser"); news.setInsertTime(new Date()); news.setUpdateTime(new Date()); news.setIsDelete(false); news.setViews(0); int insert = newsMapper.insert(news); //res.put("status","succ"); return "editDetails"; } catch (Exception e) { e.printStackTrace(); return "添加异常,请联系管理员!"; } } /** * 编辑修改 * @param news * @param bindingResult * @return */ @RequestMapping(method = RequestMethod.PUT) //@ResponseBody public String updateNews(News news,BindingResult bindingResult){// try { LOGGER.info("传入参数:" + news); news.setUpdateTime(new Date()); newsMapper.updateByPrimaryKey(news); return "editDetails"; } catch (Exception e) { e.printStackTrace(); return "更改异常,请联系管理员!"; } } /** * 删除 * @param model * @param id * @return */ @RequestMapping(method = RequestMethod.DELETE) @ResponseBody public Map<String,Object> deleteNews(Model model, String id){ Map<String,Object> res = new HashMap<String, Object>(); try { int i = newsMapper.updateIsDelePrimaryKey(!StringUtils.isEmpty(id) ? Integer.parseInt(id) : -1); System.out.println("删除结果为:"+(i==1?true:false)); res.put("status","succ"); return res; } catch (Exception e) { e.printStackTrace(); res.put("status","error"); res.put("msg",e.getMessage()); return res; } }
该种写法不够完美,写多个方法并且匹配的不是相同的url.强迫症表示不能接受
// 127.0.0.1:8080/dep/test 查询全部 // 127.0.0.1:8880/dep/test/1 查询ID为1 @ApiOperation(value="测试同时实现查询所有和根据id查询", notes="测试同时实现查询所有和根据id查询") @RequestMapping(value = {"/test/{id}","/test"},method = RequestMethod.GET) public Object deps(@PathVariable( value = "id",required = false) String id) { if(StringUtils.isEmpty(id)){ // ID为空查全部 return depService.queryAll(); }else { // 不为空根据id查询 return depService.queryById(id); } }
原文:https://www.cnblogs.com/erlongxizhu-03/p/12205454.html