koralgooll
koralgooll

Reputation: 412

Why does @RequestMapping Spring annotation in controller capture more that I want?

I have simple Spring controller with mapping:

@Controller
public class HomeController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
    public String home(HttpSession session, HttpServletRequest request, HttpServletResponse response, Model Principal principal) {
        ...
        return "home";
    }
}

It is natural that it capture http://localhost:18080/XXX/home, but why does it capture links like http://localhost:18080/XXX/home.error or http://localhost:18080/XXX/home.qwe123.234 etc. I haven't set mapping for home.error or home.qwe123.234 etc. anywhere. I have mapping only in my controllers. How to stop controller to matching that?

Upvotes: 3

Views: 838

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279890

Because, by default, Spring sets up your MVC environment with a PathMatchConfigurer which has its useSuffixPatternMatch set to true. From the javadoc

Whether to use suffix pattern match (".*") when matching patterns to requests. If enabled a method mapped to "/users" also matches to "/users.*".

The default value is true.

You can set it to false in your MVC configuration by having your @Configuration class extend WebMvcConfigurationSupport and overriding

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false);
}

Upvotes: 9

Related Questions