GET请求:

1.@PathVariable 注解:参数拼接在url上,value的方式入参

@GetMapping("/getData/{id}")
public ResponseResult test(@PathVariable List<String> id){
    return ResponseResult.ofSuccess(id);
}

集合入参,参数以逗号进行分隔
image.png

2.@RequestParam 注解:参数拼接在url上(只能用Params方式,不能用Body),以key=value的方式入参

 @GetMapping("/getData")
public ResponseResult test1(@RequestParam List<String> id){
    return ResponseResult.ofSuccess(id);
}

集合入参,参数以逗号进行分隔
image.png

3.@RequestBody 注解:由于GET的参数是通过Url方式传递的,而不是请求体传递的,所以无法通过@RequestBody注解来接收。


POST请求:

1.@PathVariable 注解:参数拼接在url上,value的方式入参

@PostMapping("/getData/{id}")
public ResponseResult test2(@PathVariable List<String> id){

    return ResponseResult.ofSuccess(id);
}

集合入参,参数以逗号进行分隔
image.png

2.@RequestParam 注解:即可拼接在url上,也可在请求体中

@PostMapping("/getData1")
public ResponseResult test3(@RequestParam List<String> id){
    return ResponseResult.ofSuccess(id);
}

使用Params方式:参数拼接在url上
image.png
使用Body,form的方式:参数在请求体中
image.png
使用Body,json的方式:参数在请求体中

会报id不能为空的错误信息

3.@RequestBody 注解:

 @PostMapping("/getData2")
public ResponseResult test4(@RequestBody List<String> id){
    return ResponseResult.ofSuccess(id);
}

不支持Params方式入参

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing:

不支持Body,form方式入参

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

支持Body,json方式
image.png


指尖上的Ken
1 声望1 粉丝