Spring 否定参数时 Rest @RequestMapping 的问题
Issue with Spring Rest @RequestMapping when negating params
我有两个 spring 控制器方法:
@RequestMapping(value="/requestotp",method = RequestMethod.POST,params = "!applicationId") //new customer
public OTPResponseDTO requestOTP( @RequestBody CustomerDTO customerDTO){
return customerService.requestOTP(customerDTO);
}
@RequestMapping(value="/requestotp",method = RequestMethod.POST,params = {"idNumber","applicationId"}) //existing customer
public String requestOTP( @RequestParam(value="idNumber") String idNumber , @RequestParam(value="applicationId") String applicationId) {
return customerService.requestOTP(idNumber, applicationId);
}
using "!applicationId"
,我期望当我用 applicationId
参数调用 url 时会调用第二个方法,但实际上当我传递这样的请求时:
{"idNumber":"345","applicationId":"64536"}
第一个方法被调用
这是我依赖的 params
参数文档的一部分:
Finally, "!myParam" style expressions indicate that the specified
parameter is not supposed to be present in the request.
你不能简单地删除第一个请求参数吗?
@RequestMapping(value="/requestotp",method = RequestMethod.POST) //new customer
public OTPResponseDTO requestOTP( @RequestBody CustomerDTO customerDTO){
return customerService.requestOTP(customerDTO);
}
问题实际上不是否定参数,问题是我在 POST 正文中发送 {"idNumber":"345","applicationId":"64536"}
并且我期望变量映射到注释的方法参数使用 @RequestParam
... 这是不正确的 ... @RequestParam
仅映射 URL 参数 ... 所以控制器试图找到最佳匹配,所以它使用第一种方法因为它包含 @RequestBody
我有两个 spring 控制器方法:
@RequestMapping(value="/requestotp",method = RequestMethod.POST,params = "!applicationId") //new customer
public OTPResponseDTO requestOTP( @RequestBody CustomerDTO customerDTO){
return customerService.requestOTP(customerDTO);
}
@RequestMapping(value="/requestotp",method = RequestMethod.POST,params = {"idNumber","applicationId"}) //existing customer
public String requestOTP( @RequestParam(value="idNumber") String idNumber , @RequestParam(value="applicationId") String applicationId) {
return customerService.requestOTP(idNumber, applicationId);
}
using "!applicationId"
,我期望当我用 applicationId
参数调用 url 时会调用第二个方法,但实际上当我传递这样的请求时:
{"idNumber":"345","applicationId":"64536"}
第一个方法被调用
这是我依赖的 params
参数文档的一部分:
Finally, "!myParam" style expressions indicate that the specified parameter is not supposed to be present in the request.
你不能简单地删除第一个请求参数吗?
@RequestMapping(value="/requestotp",method = RequestMethod.POST) //new customer
public OTPResponseDTO requestOTP( @RequestBody CustomerDTO customerDTO){
return customerService.requestOTP(customerDTO);
}
问题实际上不是否定参数,问题是我在 POST 正文中发送 {"idNumber":"345","applicationId":"64536"}
并且我期望变量映射到注释的方法参数使用 @RequestParam
... 这是不正确的 ... @RequestParam
仅映射 URL 参数 ... 所以控制器试图找到最佳匹配,所以它使用第一种方法因为它包含 @RequestBody