java.lang.IllegalStateException: Ambiguous handler methods mapped for ‘/’

What is the issue?

The spring boot application throws this error when we tried the root request mapping on multiple methods in our controller.

The error message

java.lang.IllegalStateException: Ambiguous handler methods mapped for '/': {public java.lang.String no.mentormedier.migration.controller.HomePageController.newhome(org.springframework.ui.Model), public java.lang.String no.mentormedier.migration.controller.HomePageController.home(org.springframework.ui.Model)}

How to get this error?

We tried the root request mapping {“/”} on multiple methods the code compiles correctly but at runtime it failed with the above error message. See the code that triggered the error:

    @RequestMapping({"/"})
    public String home(Model model) {
        model.addAttribute("targetEnv", activeProfile);
        return "home";
    }

    @RequestMapping({"/", "/new"})
    public String newhome(Model model) {
        model.addAttribute("targetEnv", activeProfile);
        return "home";
    }

The solution

We had to adjust the ambiguous request mapping and that solved the issue. See the code below:

    @RequestMapping({"/"})
    public String home(Model model) {
        model.addAttribute("targetEnv", activeProfile);
        return "home";
    }

    @RequestMapping({"/new"})
    public String newhome(Model model) {
        model.addAttribute("targetEnv", activeProfile);
        return "home";
    }