@PathVariable和 @RequestParam区别
1、@PathVariable 是将url中的参数和方法形参绑定
@GetMapping("/teachplan/list/{courseId}")
public TeachplanNode findTeachPlanList(@PathVariable("courseId") String courseId) {
return null ;
}
如图,完整的请求url是localhost:port/teachplan/list/123
,然后“123”作为参数传入方法中的形参courseId
2、@RequestParam 是将参数以k-v形式拼接在url后面
@RequestMapping(value = "/getuserext",method = RequestMethod.GET)
public XcUserExt getUserExt(@RequestParam("username") String username) {
return null;
}
如图,完整的请求url是localhost:port/getuserext?username=test
,然后“test”作为参数传入方法中的形参username;并且注解中设置的是“username”,那么前端传来的参数名必须username
这两个注解除了使用上是不同的,但内部都有三个属性:
value:请求参数名(必须配置)
required:是否必需,默认为 true,即 请求中必须包含该参数,如果没有包含,将会抛出异常(可选配置)
defaultValue:默认值,如果设置了该值,required 将自动设为 false,无论你是否配置了required,配置了什么值,都是 false(可选配置)