相关疑难解决方法(0)

如何在 Spring Boot REST API 中启用对 JSON / Jackson @RequestBody 的严格验证?

如果在 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)

java rest spring jackson spring-boot

3
推荐指数
1
解决办法
6859
查看次数

标签 统计

jackson ×1

java ×1

rest ×1

spring ×1

spring-boot ×1