Srpingboot中API中的注解
Springboot中@RequestParam注释post请求的参数时,默认该参数的Required=true,且该API只支持提交form形式的数据,想以Json形式提交post请求的话,可以用@RequestBody注释
以下是@PathVariable, @RequestParam和@RequestBody的区别
| 支持的类型 | 支持的Content-Type | 请求举例 | |
| @PathVariable | GET | 所有 | /test/{id} |
| @RequestParam | GET | 所有 | /test?id=1 |
| POST/PUT/DELETE/PATCH | form-data或x-www.form-urlencoded | id:1 | |
| @RequestBody | POST/PUT/DELETE/PATCH | Json | {"id":1} |
@RequestBody的使用举例:
@ApiOperation(value="check user by email",notes = "通过邮箱验证用户是否存在")
@RequestMapping(value = "/checkLogin", method = RequestMethod.POST)
public Response<String> checkLogin(
@ApiParam(value = "user info") @RequestBody User user ){
String email = user.getEmail();
String password = user.getPassword();
Response<String> result = null ;
String token = email+password+"qwertyuiosdfghjkzxcvbn";
result = Response.success(token);
return result;
}
此时访问该API的Json格式如下:
{
"email": "123",
"password": "333",
}
@ApiImplicitParam 默认required属性事false
@RequestParam 默认的required属性事true
参考:https://blog.csdn.net/qq_39506912/article/details/90107756

浙公网安备 33010602011771号