我有以下端点:
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,默认情况下在重载jackson默认行为上禁用FAIL_ON_UNKNOWN_PROPERTIES.
请参阅此link来配置弹簧.
感谢您的帮助