繁体   English   中英

如何在Spring Rest Api中的post方法中限制查询参数?

[英]How to restrict the query params in post method in Spring Rest Api?

如何在Spring Rest Api中的post方法中限制查询参数?

网址: http:// localhost:9003 / test-api / getDetails

RequestBody:

{
  "name": "TestUser",
  "age": 25,
  "Email": "abc@test.com"

}

在上面的URL中,如果存在以下get query params,我们应该抛出错误/限制http:// localhost:9003 / test-api / getDetails?dateOfBirth = 26081992

我的代码:

@RequestMapping(value = "/getDetails", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public @ResponseBody ResponseEntity<String> TestMethod(
            @Valid @RequestBody CustomerDetails customerDetails, BindingResult result,
             HttpServletRequest request) {
             //..some Actions
             }

您可以使用@RequestParam将所有查询参数绑定到Map<String, String>MultiValueMap<String, String> 文档中

如果方法参数是Map<String, String>MultiValueMap<String, String>并且未指定参数名称,则将使用所有请求参数名称和值填充map参数。

就像是:

@RequestMapping(value = "/foo", method = RequestMethod.POST)
public ResponseEntity<String> foo(@RequestParam Map<String, String> requestParams,
                                  @RequestBody Foo foo) {
    ...     
}

如果映射包含无效值,则使用400状态代码拒绝该请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM