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

编程入门 行业动态 更新时间:2024-10-28 12:21:03
本文介绍了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:拒绝具有未知字段的请求

本文发布于:2023-10-11 23:54:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483199.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   Spring   RestController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!