- GET: 获取资源
- POST:创建或更新资源
- PUT: 创建或更新资源
- DELETE:删除资源
- HEAD:获取资源的元数据,不常用
- OPTIONS:获取信息,关于资源的哪些属性是客户端可以改变的, 不常用
restful的增删改查后端Java代码:
/** * 跳转至详情编辑页面 * @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; } }
前端thymeleaf代码:
//点击确定,修改或添加 function confir(){ //获取富文本编辑器的内容,和获取普通div内容一样。 var cont = $("#editor").html(); var titleEditor = $("#titleEditor").val(); // console.log(cont); var news_id = $("#confirmBtn").attr("news_id"); var methodType = ""; if ([[${news}]] != null) { var data_news = [[${news}]];//日期字段已转换为字符串,传递到后端日期接收,或对象接收对象中共日期字段,需要转换为日期类型传递过去。已测试后端接收到日期。 methodType = 'PUT'; }else { var data_news = {}; methodType = 'POST'; } var newsInput = $("#newsInput").val(); //console.log(newsInput); console.log(data_news); data_news.content = cont; data_news.title = titleEditor;//"<b>"+titleEditor+"</b>"; var formEle = document.getElementById("myForm"); formEle.action = baseUrl+"/news";///updateNews var formData = new FormData(formEle); Object.keys(data_news).forEach((key) => { var obj = data_news[key]; if(isNaN(obj)&&!isNaN(Date.parse(obj))){ console.log("obj是日期格式!") formData.append(key, getDateByTimeStr(obj)); }else{ formData.append(key, obj); } }); console.log(formData.get("insertTime")); //formEle.submit(); //提交表单 var xhr = new XMLHttpRequest(); //设置响应返回的数据格式 xhr.responseType = "json"; if(false){ } xhr.open(methodType, baseUrl+"/news", true); xhr.onload = function () { if (xhr.status == 200) { cancel(); }else{ console.log('An error occurred!'+xhr.status); } /* xhr.ontimeout = function(e) { console.log("请求超时"); };*/ xhr.onerror = function(e) { alert(e); }; }; xhr.send(formData); }
该种写法不够完美,写多个方法并且匹配的不是相同的url.强迫症表示不能接受
@PathVariable设置为空的问题(需要设置required=false否则为空会报错)
// 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); } }