Search code examples
springspring-restspring-web

Issue with Spring Rest @RequestMapping when negating params


I have two spring controller methods :

@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" , I am expecting that when I call the url with applicationId parameter there that the second method will be called , but actually when I pass a request like this :

{"idNumber":"345","applicationId":"64536"}

The first method gets called

This is the part of the params paremeters documentation that I rely on :

Finally, "!myParam" style expressions indicate that the specified parameter is not supposed to be present in the request.


Solution

  • The issue actually wasn't with negating the parameter, the issue was that I was sending {"idNumber":"345","applicationId":"64536"} in the POST body and I was expecting the variables to be mapped to the method parameters annotated with @RequestParam ... this is not correct ... @RequestParam only map URL parameters .... so the controller was trying to find the best match so it was using the first method as it contained @RequestBody