1

I have a Spring boot app where I have an API that takes other urls as path params. For example:

host:port/{eid} is my base path and after this I can have URLs like

host:port/{eid}/abc
host:port/{eid}/abc/pqr/
host:port/{eid}/abc/pqr/b=2
host:port/{eid}/abc/pqr/xyz
host:port/{eid}/abc/pqr/xyz?a=1

...and so on...

I would like to define a controller that I can map to all the above URLs and that should work something like

@RequestMapping(value = "/{eid}/{urlParts}", method = RequestMethod.GET)
public ResponseEntity<Object> share(
        @PathVariable String eid, 
        @PathVariable String urlParts) {
 ...... 
}

I tried using @PathVariable Map<String, String> path and also @RequestMapping(value = "/{eid}/{urlParts:.+}"

but couldn't get the expected result.

Is there any solution to receive path slash(/) in path param.

Note: I can not URL encode the slash(/) in the URL. That's not an option for me.

BiJ
  • 1,639
  • 5
  • 24
  • 55

4 Answers4

3

I know the query is too old but still it's useful and this answer can help others. You can get the full url parts using request attribute as below.

@RequestMapping(value = "/{eid}/**", method = RequestMethod.GET)
public ResponseEntity<Object> share(@PathVariable String eid, HttpServletRequest request) {

    Object uriObject = request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    if (null != uriObject) {
        String urlParts = uriObject.toString().replaceFirst("^/" eid + "/", "");
    }
    ....
}
Vinit Solanki
  • 1,863
  • 2
  • 15
  • 29
  • Awesome solution! It's really appreciable and saved my day. Thank you [Vinit Solanki](https://stackoverflow.com/users/5404976/). – Śhāhēēd Nov 15 '22 at 02:38
0

why don't you try @RequestParam to take url if you working with jsp or other stuff..

@PathVariable means that the annotated method argument should be extracted from the path of the invoked URL. @RequestParam means that the annotated method argument must be extracted from the request parameters. None of these annotations cause the annotated arguments to be put in the request, session or application scope.

so you use your map also...

${username} means "write the value of the username attribute (found in page, or request, or session, or application scope) in the response". Since you didn't include any username attribute in any of those scopes, it doesn't write anything.

The code would work if the method returned a ModelAndView object, and the model contained a username attribute and a studentid attribute.

  • Thought of using `@RequestParam` but the requirement is as mentioned above. Can't use a `@RequestParam` – BiJ Jul 21 '17 at 07:10
0

you can refer below code and link :

First URL : localhost:8080/projectName/test?firstname=john

Second URL :localhost:8080/projectName/test?firstname=john&secondname=roy

    @Controller
    @RequestMapping("/test")
    public class TestController {




    @RequestMapping(value = { "/test/{firstname}/test" }, method = { RequestMethod.GET })
public String someMethod(@PathVariable("firstname") String firstname){
    return someMethod(firstValue )

}

    @RequestMapping(value = { "/test/{firstname}/{otherString}/test" }, method = { RequestMethod.GET })
public String someOtherMethod(@PathVariable("firstname") String firstname, @PathVariable("secondname") String secondValue) {
    return someMethod(firstValue + "/" + secondValue)

 }
}
Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
  • Like I mentioned earlier the urlPart could have 2 or 200 slashes. I need a generic approach – BiJ Jul 21 '17 at 07:12
0

so I am not sure if there is a direct spring implementation to doing this however, you could us a mixture of things.

  • @RequestParam - returns a map of the URL params (succeeding the ?)
  • @PathVariable - return the eid
  • HttpServletRequest - use the request to return the URI and strip host:port/{eid} and anything after ? , then use Arrays.asList(str.split("/")); (remember this is a wrapper of an array use new ArrayList<Sting>(Arrays.asList(str.split("/"))) )

    @RequestMapping(value = "/{eid}", method = RequestMethod.GET)
      public ResponseEntity<Object> share(
      @PathVariable String eid, 
      @RequestParam Map<String,String> allRequestParams,
      HttpServletRequest request) {
      ...... 
    }
    
dCrux
  • 121
  • 5