Kishore
Kishore

Reputation: 83

RequestMapping in spring with weird patterns

I have defined the controller method in spring like following

@RequestMapping(value = "/register", method = RequestMethod.GET)
public ModelAndView register(HttpSession session) {    

and when i try to access the register page it is working fine.

localhost:8080/myapp/register

but the issue is it is giving me the same page with all these patterns

localhost:8080/myapp/register.htm
localhost:8080/myapp/register.abc
localhost:8080/myapp/register.pqrasdfdadf

i want to stop this behaviour. can anyone suggest ?

Upvotes: 1

Views: 380

Answers (2)

Diptopol Dam
Diptopol Dam

Reputation: 815

I think if you use @RequestMapping(value = "/register/", method = RequestMethod.GET) , you can easily solve your problem.

Another Solution:

Use regular expression.

@RequestMapping(value = "{varName:/register$}")

Here $ represents the end of the string.

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280172

Depending on how you do your Web MVC configuration, you have to set the RequestMappingHandlerMapping useSuffixPatternMatch property to false.

If you're doing your configuration through a WebMvcConfigurationSupport sub class, simply do

@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
    RequestMappingHandlerMapping mapping = super.requestMappingHandlerMapping();
    mapping.setUseSuffixPatternMatch(false);
    return mapping;
}

With XML configuration, you'll need to explicitly declare a RequestMappingHandlerMapping with id requestMappingHandlerMapping and set the corresponding <property>.

This property, which is true by default, determines

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

Upvotes: 4

Related Questions