Search code examples
javaspring-mvcurl-mapping

Spring MVC N number of parameters mapping


I tried binding N numbers of parameters in a URL like this

/web/appl/<applname>/rule/<rulename>/<attrname1>/<attrval1>/<attrname2>/<attrval2>/.../<attrnameN>/<attrvalN>

with

@RequestMapping(value = "/web/appl/{applname}/rule/{rulename}/{attributes}", method = RequestMethod.GET)
public Object GetService(HttpServletRequest request, @PathVariable("attributes") Map<String, Object> map,
                @PathVariable("applname") String applname, @PathVariable("rulename") String rulename)
                throws Exception {
                  ...
                }

but could not get values of <attrval1>/<attrname2>/<attrval2>/.../<attrnameN>/<attrvalN>


Solution

  • Use

    String urlAttributes = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);

    and get whole URL. After that parse this URL according to your needs like if you did not get

    <attrval1>/<attrname2>/<attrval2>/.../<attrnameN>/<attrvalN>

    in proper sequence then you can throw exception.