Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

310
Visualizações
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?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda