4

Small question regarding how to get the Spring request mapping (GET mapping, POST mapping...), the path (route) parameter(s) as variable(s). A bit of background, we currently have a use case where we take a route, compute some information, (the Disaster Recovery URL, the test URL, the most available URL, but the question is not here) and respond back. This is just a partial example to have something a bit more concrete in the question.

Query at http://the-main-host.com/firstRoute We return http://go-to-region-us-west3.com/firstRoute and maybe minutes later, we return http://go-to-region-eu-central.com/firstRoute , but again, this is not the question.

@GetMapping(path = "/{id}/firstRoute", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<String> question(@PathVariable @NotBlank final String id, String url) {
    String theUrlEnrichedWithOtherInformation = computeExtraInformationForURL(id, url);
    return Mono.just(theUrlEnrichedWithOtherInformation + id + "/firstRoute");
}

With time, we are now at some 300 of those handlers, all with real use cases

@PostMapping(path = "/{id}/twoHundredAndFiveRoute", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<String> question(@PathVariable @NotBlank final String id, String url) {
    String theUrlEnrichedWithOtherInformation = computeExtraInformationForURL(id, url);
    return Mono.just(theUrlEnrichedWithOtherInformation + id + "/twoHundredAndFiveRoute");
}

@GetMapping(path = "/{id}/twoHundredSixtySevenRoute", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<String> question(@PathVariable @NotBlank final String id, String url) {
    String theUrlEnrichedWithOtherInformation = computeExtraInformationForURL(id, url);
    return Mono.just(theUrlEnrichedWithOtherInformation + id + "/twoHundredSixtySevenRoute");
}

We managed to make it more maintainable by using the Spring array of the path annotation. This way of doing is a bit nicer, it brought down our two hundred methods down to a single digit. We would like to keep using this way is possible. However, we lose the information what was the path being invoked.

@GetMapping(path = {"/{id}/firstRoute", "/{id}/twoHundredSixtySevenRoute"}, produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<String> question(@PathVariable @NotBlank final String id, String url) {
        String theUrlEnrichedWithOtherInformation = computeExtraInformationForURL(id, url);
        return Mono.just(theUrlEnrichedWithOtherInformation + id + getThePathThatWasUsedFromTheArrayPlease());
    }

Is it possible to get it back, like in this speculative example?

12
  • I don't understand your question. Commented Nov 26, 2020 at 3:44
  • I think I understood you. Due to your @GetMapping has a lot of paths,do you need to get the real path that is being invoked? Commented Nov 26, 2020 at 3:56
  • Can't you use requestparam to differentiate the different paths? Commented Nov 26, 2020 at 3:58
  • Wouldn't you just do something like path="{path}" and map it to a variable as question(@PathVariable String path)? Commented Nov 26, 2020 at 4:09
  • 1
    @PatPatPat its in the comment. set your path to "{path}" and your method would be Mono<String> question(@PathVariable String path) Commented Nov 26, 2020 at 4:31

2 Answers 2

4

What you need is to access the HttpServletRequest instance. This has a lot of information about http invocation.

Spring allow us an easy way to access to this instance:

@RequestMapping(value = "/report/{objectId}", method = RequestMethod.GET)
public @ResponseBody void generateReport(
        @PathVariable("objectId") Long objectId, 
        HttpServletRequest request) {

}

If you have access to HttpServletRequest, you can get any part of the url:

Example: http://myhost:8080/people?lastname=Fox&age=30

String uri = request.getScheme() + "://" +   // "http" + "://
             request.getServerName() +       // "myhost"
             ":" +                           // ":"
             request.getServerPort() +       // "8080"
             request.getRequestURI() +       // "/people"
             "?" +                           // "?"
             request.getQueryString();       // "lastname=Fox&age=30"

request.getRequestURI() should have the path value that you need.

Sign up to request clarification or add additional context in comments.

1 Comment

I was sure there were a smart way to do it that I didn't knew. That's much better than what I have now. Thanks. However, I am in a reactive world, so it is not a straightforward for HttpServletRequest
0

You can get it from HttpServletRequest - request.getRequestURI(). Either autowire it or use it from method parameter.

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.