restful风格url Get请求查询所有和根据id查询的合并成一个controller的方法
原代码
?? // 127.0.0.1:8080/dep/s
@ApiOperation(value="查询所有", notes="查询所有") ???@RequestMapping(value = "/s",method = RequestMethod.POST) ???public List<Dep> deps() { ???????return depService.queryAll(); ???}
// 127.0.0.1:8080/dep/1 ???@GetMapping(value = "/{id}") ???public Dep get(@PathVariable Long id){ ???????return depService.queryById(id); ???}
该种写法不够完美,写多个方法并且匹配的不是相同的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); ???????} ???}
也可以直接映射两个不同方法,这里是不同的url映射同一个方法
必须匹配多个url时requird = false才能生效(我猜的!)
参考[1].https://www.imooc.com/qadetail/268268?t=436798
restful风格url Get请求查询所有和根据id查询的合并成一个controller
原文地址:https://www.cnblogs.com/covet/p/10141617.html