简体   繁体   English

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

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

How to restrict the query params in post method in Spring Rest Api? 如何在Spring Rest Api中的post方法中限制查询参数?

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

RequestBody: RequestBody:

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

}

In the above URL, if there is get query params as below , we should throw error/ restrict http://localhost:9003/test-api/getDetails?dateOfBirth=26081992 在上面的URL中,如果存在以下get query params,我们应该抛出错误/限制http:// localhost:9003 / test-api / getDetails?dateOfBirth = 26081992

My Code: 我的代码:

@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
             }

You could bind all query parameters to a Map<String, String> or MultiValueMap<String, String> using @RequestParam . 您可以使用@RequestParam将所有查询参数绑定到Map<String, String>MultiValueMap<String, String> From the documentation : 文档中

If the method parameter is Map<String, String> or MultiValueMap<String, String> and a parameter name is not specified, then the map parameter is populated with all request parameter names and values. 如果方法参数是Map<String, String>MultiValueMap<String, String>并且未指定参数名称,则将使用所有请求参数名称和值填充map参数。

Something like: 就像是:

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

Then refuse the request with a 400 status code if the map contains invalid values. 如果映射包含无效值,则使用400状态代码拒绝该请求。

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

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