如果请求同一个资源,但是参数不同的怎么办?
像上面这两种情况的话,请求同一个列表,只是传参不一样 。如果path写一样的会报错,按照规范,这个path应该怎么命名呢?
如果请求同一个资源,但是参数不同的怎么办?
像上面这两种情况的话,请求同一个列表,只是传参不一样 。如果path写一样的会报错,按照规范,这个path应该怎么命名呢?
/info/list?classType=?
/info/list?courseId=?
这种都是写一起的吧
第一种
@RequestMapping(path = "/info/list/{classType}/{courseId}", method = RequestMethod.GET)
public Info getInfoList(@PathVariable String classType, @PathVariable String courseId) {
// code here
}
第二种
@RequestMapping(path = "/info/list", method = RequestMethod.GET)
public Info getInfoList(@RequestParam String classType, @RequestParam String courseId) {
// code here
}
还有一种通用的方式,参照下面
比如你想设计的restful api是这样的,
http://localhost:2000/custom?brand=dell&limit=20&price=20000&sort=asc
那么想获得查询参数,可以采用map来获取,
@RequestMapping(method = RequestMethod.GET, value = "/custom")
public String controllerMethod(@RequestParam Map<String, String> customQuery) {
System.out.println("customQuery = brand " + customQuery.containsKey("brand"));
System.out.println("customQuery = limit " + customQuery.containsKey("limit"));
System.out.println("customQuery = price " + customQuery.containsKey("price"));
System.out.println("customQuery = other " + customQuery.containsKey("other"));
System.out.println("customQuery = sort " + customQuery.containsKey("sort"));
return customQuery.toString();
}
希望能帮到你。
8 回答6.6k 阅读
4 回答715 阅读✓ 已解决
2 回答3.4k 阅读
3 回答1.9k 阅读✓ 已解决
1 回答2.2k 阅读✓ 已解决
1 回答2.1k 阅读✓ 已解决
1 回答974 阅读✓ 已解决
基本的CRUD可按照resultFul来命名。其他功能可自定义,不用满足resultFul规范