Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

java - Spring Boot Ambiguous handler error because of unvalidated URL parameters

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...