rhinds
rhinds

Reputation: 10043

Spring boot - Controller catching all URLs

I am building a Spring boot web app and am using annotations for controller/url mapping.

I have several controllers annotated with @RequestMapping with the url value set (both empty strings and specific URLs) which are working fine e.g.

@Controller
@RequestMapping("/accounts")
class SignInController {

    @Autowired PartyService partyService

    @RequestMapping(value="", method = RequestMethod.GET )
    public String signinPage( Model model) {

Navigating to /accounts renders the sign-in page correctly.

However, if I add a controller with no RequestMapping values e.g.

@Controller
class CustomController {

    @RequestMapping
    public String transform( Model model ) {

Then any URL I enter that doesn't match any other specific controller is getting handled by this controller (so pages I would expect to 404 all just renderthis page). Is this expected behaviour? I was not expecting this, and as the RequestMapping value defaults to empty and is an antMatcher I wouldn't have thought it would handle all other URLs.

The reason I have this controller with out RequestMapping defined is because I want to also have a SimpleUrlMappingHandler defined with some explicit URLs going to that controller, and if I don't include the @Controller & @RequestMapping annotations to that controller then I get an error about not being able to find the handler method (maybe the problem is that I have mis-understood the implementation details of that).

Should my custom controller be handling all URLs? If so, is there something I can do so it doesnt and only gets called for the explicit SimpleUrlMappingHandler I have defined?

Upvotes: 2

Views: 3322

Answers (1)

rhinds
rhinds

Reputation: 10043

As mentioned in the comments - I just removed the @Controller annotation from the class, and then explicitly defined the controller as a @Bean in my config class and explicitly assigned that to the mapping in the SimpleUrlMappingHandler configuration

Upvotes: 2

Related Questions