Java 如何在SpringRESTAPI中限制post方法中的查询参数?
如何在SpringRESTAPI中限制post方法中的查询参数 网址: 请求主体:Java 如何在SpringRESTAPI中限制post方法中的查询参数?,java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,如何在SpringRESTAPI中限制post方法中的查询参数 网址: 请求主体: { "name": "TestUser", "age": 25, "Email": "abc@test.com" } 在上面的URL中,如果有下面的get查询参数,我们应该抛出error/restrict 我的代码: @RequestMapping(value = "/getDetails", method = RequestMethod.POST, produces = "applicatio
{
"name": "TestUser",
"age": 25,
"Email": "abc@test.com"
}
在上面的URL中,如果有下面的get查询参数,我们应该抛出error/restrict
我的代码:
@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
}
@RequestMapping(value=“/getDetails”,method=RequestMethod.POST,products=“application/json;charset=UTF-8”)
public@ResponseBody ResponseEntity测试方法(
@有效@RequestBody CustomerDetails CustomerDetails,BindingResult,
HttpServletRequest(请求){
//…一些行动
}
您可以将所有查询参数绑定到或使用。从:
如果方法参数为或且未指定参数名称,则映射参数将填充所有请求参数名称和值
比如:
@RequestMapping(value=“/foo”,method=RequestMethod.POST)
公共响应foo(@RequestParam映射requestParams,
@请求主体(Foo-Foo){
...
}
如果映射包含无效值,则使用
400
状态代码拒绝请求。为什么?无论用户做什么,您都可以控制控制器中使用的参数。在执行POST或GET时,您是否可以这样做?为什么不直接忽略查询参数?@EssexBoy没错。。但是在这种情况下,如果用户发送的是应用程序不需要的查询参数(它只是忽略了),我们能抛出一个错误吗?@CássioMazzochiMolin是的,我们可以,但在我的场景中,我需要处理/检索它并抛出错误