@RequestParam,@PathVariable 向右看齐 2022-04-04 05:46 272阅读 0赞 **`@RequestParam` 和 `@PathVariable` 注解都是用于从request中接收请求,两个都可以接收参数,** ### 不同:`@RequestParam` 从request里面拿取值,`@PathVariable` 从一个URI模板里面来填充 ### **演示:** Request URL:http://api.baidu.com/api/item/spec/groups?param1=3¶m2=4 ### ***`@RequestParam` 是从request里面拿取值,*** ### public String getDetails( @RequestParam(value="param1", required=true) String param1, @RequestParam(value="param2", required=false) String param2){ ... } ### ***`@PathVariable` 是从一个URI模板里面来填充*** ### Request URL:http://api.baidu.com/api/item/spec/groups/79 @RestController @RequestMapping("spec") public class SpecificationController { @Autowired private SpecificationService specificationService; @GetMapping("groups/{cid}") public ResponseEntity<List<SpecGroup>> querySpecGroups(@PathVariable("cid") Long cid){ List<SpecGroup> list = this.specificationService.querySpecGroups(cid); if(list == null || list.size() == 0){ return new ResponseEntity<>(HttpStatus.NOT_FOUND); } return ResponseEntity.ok(list); } }
还没有评论,来说两句吧...