Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC Controller mapping regex

My controller mappings:

@RequestMapping(value = "{objectId}/{objectName: [a-zA-Z-]+}", method = GET)
public String getObjects(@PathVariable Integer objectId, Model model) { ... }

@RequestMapping(value = "{objectId}/{objectName: [a-zA-Z-]+}_{category}", method = GET)
public String getObjectsForCategories(@PathVariable Integer objectId, 
                                      @PathVariable String category, 
                                      Model model) { ... } 

And the urls I'm hitting:

http://localhost:8080/objects/2/xyz

http://localhost:8080/objects/2/xyz_mobile

Spring unable to find the handler and complains with No mapping found for HTTP request.

like image 485
Shwetanka Avatar asked Oct 28 '25 16:10

Shwetanka


1 Answers

According to the Spring MVC documentation:

The @RequestMapping annotation supports the use of regular expressions in URI template variables. The syntax is {varName:regex} where the first part defines the variable name and the second - the regular expression.

So, you should remove the space between : and regex, like the following:

@RequestMapping(value = "{objectId}/{objectName:[a-zA-Z-]+}", method = GET)
                                                ^ Space is removed
like image 126
Ali Dehghani Avatar answered Oct 31 '25 07:10

Ali Dehghani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!