5

Consider a Spring MVC @RestController with a @GetMapping which maps to /v1/** to handle all sub-paths.

@RestController
public class MyController {

    @GetMapping(value = "/v1/**", produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView handle(String fullPathInMethodArg) {
        ...
        String fullPathInLocalVar = ...;
        ...
    }
}

How can I retreive the full path?

Ideally, I want to retrieve it as an argument in the method: String fullPathInMethodArg.

Alternatively, I want to retrieve it as a local variable: String fullPathInLocalVar.

Stefaan Neyts
  • 2,054
  • 1
  • 16
  • 25

1 Answers1

8

I think this answers your question:

@RequestMapping("/v1/**")
public void foo(HttpServletRequest request) {
    String fullPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String subPath = StringUtils.removeStart(fullPath, "/v1");
    ...
}

Spring 3 RequestMapping: Get path value

Stefaan Neyts
  • 2,054
  • 1
  • 16
  • 25
ixeption
  • 1,972
  • 1
  • 13
  • 19