Spring RestController:拒绝带有未知字段的请求

Max*_*met 9 java validation rest spring-mvc jackson

我有以下端点:

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
public class TestController {

    @RequestMapping(value = "/persons", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    public ResponseEntity<Integer> create(@RequestBody Person person) {
        // create person and return id
    }
}
Run Code Online (Sandbox Code Playgroud)

今天,如果我收到一个带有未知字段的请求,如下所示:

{
    "name" : "Pete",
    "bijsdf" : 51
}
Run Code Online (Sandbox Code Playgroud)

我创建了这个人并忽略了未知字段。

如何检查是否存在未知字段,然后返回错误请求?

Max*_*met 6

Spring (4.1.2-RELEASE) 使用它的Jackson2ObjectMapperBuilder,默认情况下禁用 FAIL_ON_UNKNOWN_PROPERTIES 重载杰克逊默认行为。请参阅此链接以配置 spring。感谢您的帮助