I have two methods in my controller class with two unique URLs.
The first one has one optional parameter called name, which is a required parameter in the second URL. This seems to confuse Spring Boot when used.
First method:
@GetMapping(value = "/", params= {"id", "applicantid", "startingdate", "endingdate"})
public List<Event> getEventsById(@RequestParam("id") String sourceid,
@RequestParam("applicantid") String applicantid,
@RequestParam("startingdate") String startingdate,
@RequestParam("endingdate") String endingdate,
@RequestParam(value = "name", required = false) String name) {
The second one:
@GetMapping(value = "/", params= {"applicantid", "name", "startingdate", "endingdate"})
public List<Event> getEventsByApplicantId(@RequestParam("applicantid") String applicantid,
@RequestParam("name") String name,
@RequestParam("startingdate") String startingdate,
@RequestParam("endingdate") String endingdate) {
Now they both work fine, unless I add the optional parameter to the first URL like so:
/?id=999&applicantid=1&startingdate=2020&endingdate=2021&name=mobileapp
Spring Boot somehow thinks that I am trying to use the second method and it ignores the parameter "id" when parameter "name" is added, and gives me Ambiguous handler error. This is ofc because when "id" is ignored, the URL fits with the second one.
Shouldn't the "id" param be sufficient enough for Spring Boot to understand what method should be used?
I also noticed, that I could add all bunch of random parameters, and Spring Boot would always choose the second URL.
Do I need to validate the parameters, so that unknown parameters can't be added and Spring would not ignore the "id" parameter?
question from:
https://stackoverflow.com/questions/65847364/spring-boot-ambiguous-handler-error-because-of-unvalidated-url-parameters 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…