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
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 /.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With