3

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?

2 Answers 2

1

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());
}
Sign up to request clarification or add additional context in comments.

Comments

0

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");
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.