如果在 JSON 请求中指定了额外的参数,我该如何抛出错误?例如,“xxx”不是有效参数或在@RequestBody对象中。
$ curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"apiKey": "'$APIKEY'", "email": "name@example.com ", "xxx": "yyy" }' 本地主机:8080/api/v2/stats
我尝试添加@Validated到界面,但没有帮助。
@RequestMapping(value = "/api/v2/stats", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<DataResponse> stats(Principal principal, @Validated @RequestBody ApiParams apiParams) throws ApiException;
Run Code Online (Sandbox Code Playgroud)
我想启用“严格”模式,以便在请求中存在额外的虚假参数时会出错。我找不到办法做到这一点。我找到了确保有效参数确实存在的方法,但无法确保没有额外的参数。
public class ApiParams extends Keyable {
@ApiModelProperty(notes = "email of user", required = true)
String email;
Run Code Online (Sandbox Code Playgroud)
public abstract class Keyable {
@ApiModelProperty(notes = "API key", required = true)
@NotNull
String apiKey; …Run Code Online (Sandbox Code Playgroud)