PatPanda
PatPanda

Reputation: 5042

SpringBoot - RequestMapping get the path as variable?

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?

Upvotes: 4

Views: 4123

Answers (2)

Anshuman
Anshuman

Reputation: 11

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

Upvotes: 0

JRichardsz
JRichardsz

Reputation: 16574

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.

Upvotes: 4

Related Questions