Question

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>

Was it helpful?

Solution 2

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.

OTHER TIPS

Unfortunately Spring MVC does not provide such a solution. You can consider the Matrix Variables as an alternative.

If you prefer sticking to your current URI scheme then you have to implement a solution yourself. One approach is to use a path pattern. Example:

@RequestMapping(value = "/web/appl/{applname}/rule/{rule name}/**")
public Object getService(HttpServletRequest request,
            @PathVariable("applname") String applname ...) {
    String attributesPart = new AntPathMatcher()
        .extractPathWithinPattern("/web/appl/{applname}/rule/{rule name}/**", 
            request.getServletPath());
    ...

You could implement your argument resolver that does that. Something like

@RequestMapping(value = "/web/appl/{applname}/rule/{rule name}/**")
public Object getService(@MyAttributes Map<String, String> attributes,
            @PathVariable("applname") String applname ...) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top