简体   繁体   中英

Dynamic parameters of @PathVariable in spring-mvc?

I'm looking for a way to use multiple path parameters as one string.

The following mapping is quite clear: it defines 3 static parameters as path variables:

@RequestMapping(value = "/rest/{language}/{country}/{term}?someparams=test&...", method = RequestMethod.GET)

But I want to have anything between /rest and {term} to be written into one single @PathVariable`.

Example: I could call localhost:8080/rest/this/is/my/dynamic/customterm?someparams)...

Here I'd like to get /this/is/my/dynamic as one single path variable.

The following does not work:

@RequestMapping(value = "/rest/{multiplePathParams}/{term}someparams=test&...", method = RequestMethod.GET)
public void test(@PathVariable String multiplePathParams, @PathVariableString term) {
       Assert.assertEquals(multiplePathParams, "/this/is/my/dynamic");
       Assert.assertEquals(term, "customterm");
}

Is it possible at all?

Spring url mapping is good. but this question your url is bad. your controller url is only get pattern. point is @RequestParam.

sample code

@RequestMapping(value = "/rest/{multiplePathParams}/{term}", method = RequestMethod.GET)
    public void test(@PathVariable String multiplePathParams, @PathVariableString term, @RequestParam String someparams) {
           Assert.assertEquals(multiplePathParams, "/this/is/my/dynamic");
           Assert.assertEquals(term, "customterm");
    }

It turned out not being possible to retrieve a substring inside the rest/get query.

But it's possible to extract the path matched against a wildcard:

@GetMapping(value = "/rest/**",...
public Rsp test(HttpServletReq req) {
private String getSqlTemplateKey(HttpServletRequest req) {
    String pattern = (String) req.getAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE);
    String urlpart = PATH_MATCHER.extractPathWithinPattern(pattern, req.getServletPath());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM