我有以下端点:
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
}
}
今天,如果我收到这样一个带有未知字段的请求:
{
"name" : "Pete",
"bijsdf" : 51
}
我创建了人并忽略了未知领域。
如何检查是否存在未知字段然后返回错误请求?
最佳答案
Spring (4.1.2-RELEASE) 使用它的 Jackson2ObjectMapperBuilder默认情况下禁用 FAIL_ON_UNKNOWN_PROPERTIES 重载 jackson 默认行为。 看这个link配置 Spring 。 谢谢大家的帮助
关于java - Spring RestController : reject request with unknown fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29520853/