场景
无论是后端开发的接口测试,还是前端的联调时的接口测试,在使用postman时会疑惑,选择参数时,究竟是选择raw还是x-www-form-urlencoded。
这涉及到后端开发是两个注解的使用
@RequestParam
@RequestParam注解用在参数来自于请求头的接口,拼接在url上,多数使用在GET请求上。 有三个参数:
required表示是否必填,默认为true。defaultValue可设置请求参数的默认值。value为接收url的参数名(相当于key值)。
@GetMapping("/test")
public Result test(@RequestParam(value="type", required = false, defaultValue="1") String type){
....
return Result.success();
}
请求头内容格式
@RequestParam用于处理Content-Type为application/x-www-form-urlencoded编码请求内容。此时postman中就可以在Body中选择application/x-www-form-urlencoded
@RequestBody注解
@RequestBody注解用在参数来自于请求体中的接口,requestBody。
请求头内容格式
@RequestBody多数用于处理请求头内容格式为application/json等数据,此时即可在postman中选择raw、json