'Switch endpoint responses based on condition

I would like to have two endpoints with the same path and decide which one is enabled on startup.

To do so I've tried using @ConditionalOnExpression() but an error is thrown as it says a mapping already exists. One endpoint uses ModelAndView while the other provides a html string so I can't add an if statement in the body of the endpoint.

Code

@ConditionalOnExpression("${my-property:false}")
@GetMapping(VIEW_INDEX)
public ModelAndView index(HttpServletRequest request) {
    ...
    return modelAndView;
}

@ConditionalOnProperty("${my-property}")
@GetMapping(VIEW_INDEX)
public String otherIndex(){
    return "/other/index";
}

Error

Ambiguous mapping. Cannot map 'controller' method 

There is already 'controller' bean method

How can I allow only one to be enabled based on a condition without there being an Ambiguous mapping?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source