RESTful
1.每个URI代表一种资源
2.客户端与服务器之间,传递这种资源的某种表现层
3.客户端通过HTTP动词,对服务器端资源进行操作,实现“表现层状态转换”
RESTful的应用相对广泛,在豆瓣网上也有所应用
-----------------------------------------------------------
但并不是所有的接口都适用于RESTful的,例如我们填写个人信息/收货地址的时候字段特别多。
基础代码理解:
@RequestMapping(value = "/book",method = RequestMethod.GET) @ResponseBody public String book(HttpServletRequest request){ String contentType = request.getContentType(); if(contentType == null){ return "book.default"; }else if(contentType.equals("txt")){ return "book.txt"; }else if(contentType.equals("html")){ return "book.html"; } return "book.default";
}
@RequestMapping(value = "/subject/{subjectId}",method = RequestMethod.GET) @ResponseBody public String subjectGet(@PathVariable("subjectId") String subjectId) { return "this is a get method;subjectId:"+subjectId; }
@RequestMapping(value = "/subject/{subjectId}",method = RequestMethod.POST) @ResponseBody public String subjectPost(@PathVariable("subjectId") String subjectId) { return "this is a Post method;subjectId:"+subjectId;
}
|
在做多参数的时候,我们应该尽量避免相同URI,浏览器并不能区分开来传递的参数所匹配的接口。
例:
/* 这两个方法会出现浏览器找不到匹配的接口(模棱两可) @RequestMapping(value = "/{categoryId}/{pageNum}/{pageSize}/{orderBy}",method = RequestMethod.GET) @ResponseBody public ServerResponse<PageInfo> listRESTfulBadcase(@PathVariable(value = "categoryId")Integer categoryId, @PathVariable(value = "pageNum")Integer pageNum, @PathVariable(value = "pageSize")Integer pageSize, @PathVariable(value = "orderBy")String orderBy){ if(pageNum == null){pageNum=1;} if(pageSize == null){pageSize=10;} if(StringUtils.isBlank(orderBy)){orderBy="price_asc";} return iProductService.getProductByKeywordCategory("",categoryId,pageNum,pageSize,orderBy); }
@RequestMapping(value = "/{keyword}/{pageNum}/{pageSize}/{orderBy}",method = RequestMethod.GET) @ResponseBody public ServerResponse<PageInfo> listRESTfulBadcase(@PathVariable(value = "keyword")String keyword, @PathVariable(value = "pageNum")Integer pageNum, @PathVariable(value = "pageSize")Integer pageSize, @PathVariable(value = "orderBy")String orderBy){
if(pageNum == null){pageNum=1;} if(pageSize == null){pageSize=10;} if(StringUtils.isBlank(orderBy)){orderBy="price_asc";} return iProductService.getProductByKeywordCategory(keyword,null,pageNum,pageSize,orderBy); } */
|
如果非要做多参数,那么正确的解决方法如下:
//RESTful改进版 //http://www.kerry.link/product/keyword/手机/1/10/price_asc @RequestMapping(value = "/keyword/{keyword}/{pageNum}/{pageSize}/{orderBy}",method = RequestMethod.GET) @ResponseBody public ServerResponse<PageInfo> listRESTful(@PathVariable(value = "keyword")String keyword, @PathVariable(value = "pageNum")Integer pageNum, @PathVariable(value = "pageSize")Integer pageSize, @PathVariable(value = "orderBy")String orderBy){
if(pageNum == null){pageNum=1;} if(pageSize == null){pageSize=10;} if(StringUtils.isBlank(orderBy)){orderBy="price_asc";} return iProductService.getProductByKeywordCategory(keyword,null,pageNum,pageSize,orderBy); } //RESTful改进版 //http://www.kerry.link/product/category/100012/1/10/price_asc @RequestMapping(value = "/category/{categoryId}/{pageNum}/{pageSize}/{orderBy}",method = RequestMethod.GET)//列表 @ResponseBody public ServerResponse<PageInfo> listRESTful(@PathVariable(value = "categoryId")Integer categoryId, @PathVariable(value = "pageNum")Integer pageNum, @PathVariable(value = "pageSize")Integer pageSize, @PathVariable(value = "orderBy")String orderBy){ if(pageNum == null){pageNum=1;} if(pageSize == null){pageSize=10;} if(StringUtils.isBlank(orderBy)){orderBy="price_asc";} return iProductService.getProductByKeywordCategory("",categoryId,pageNum,pageSize,orderBy); }
|