本文介绍了Spring RestController:拒绝具有未知字段的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下端点:
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
}
}
今天,如果我收到带有这样未知字段的请求:
Today if I received a request with an unknown field like this :
{
"name" : "Pete",
"bijsdf" : 51
}
我创建了这个人,而忽略了未知字段.
I create the person and ignore the unknown field.
如何检查是否存在未知字段,然后返回错误的请求?
How can I check that there's an unknown field and then return a bad request ?
推荐答案
Spring(4.1.2-RELEASE)使用 Jackson2ObjectMapperBuilder ,默认情况下,在重载杰克逊默认行为下,禁用FAIL_ON_UNKNOWN_PROPERTIES.参见此链接配置弹簧.感谢您的帮助
Spring (4.1.2-RELEASE) use it's Jackson2ObjectMapperBuilder that by default disable FAIL_ON_UNKNOWN_PROPERTIES on overload jackson default behaviour.See this link to configure spring.Thx all for your helps
这篇关于Spring RestController:拒绝具有未知字段的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!