Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@RequestMapping regular expression

I'm trying to make value atribute for spring @RequestMapping annotation to map url like this

/educationDistrict/308/action/resetAddressesForYear/1

and this

/educationDistrict/308/action/resetAddressesForYear

I'h have this

@RequestMapping(value = "/{repository}/{id}/action/{methodName:[A-z]*}{v:.*}", method = RequestMethod.POST)

but first url does not match.

I can't use multi value because of spring-hateoas https://github.com/spring-projects/spring-hateoas/issues/186

spring 4.1.5

like image 671
Pavel Varchenko Avatar asked Mar 21 '26 11:03

Pavel Varchenko


1 Answers

Add /** at the end of the URL mapping in @RequestMapping. And you can retrieve last part of the URL as below:

@RequestMapping(value = "/{repository}/{id}/action/{methodName:[A-z]*}{v:.*}/**", method = RequestMethod.GET)
public ModelAndView welcome(@PathVariable("methodName") String name, HttpServletRequest request) {

    String mvcPath = (String) request.getAttribute(
            HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    int index = StringUtils.lastIndexOf(mvcPath, "/");

    System.out.println("Method name - " + name);        
    System.out.println("Rest of the URL - " + mvcPath.substring(index+1));

    ModelAndView model = new ModelAndView();
    model.setViewName("index");
    model.addObject("name", mvcPath);

    return model;
}

Note: I have used StringUtils Apache Commons to find the last index of /.

like image 98
Mithun Avatar answered Mar 23 '26 05:03

Mithun



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!