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

294
Visualizações
Get @RequestMapping value of different method in same controller

Using Spring Boot, I have a couple methods in my RegisterController which handles new user registration.

The createNewUser method is responsible for saving the new user to the database and sending a confirmation e-mail containing a link that has a unique token.

The confirmUser method handles processing the GET request for the confirmation link.

Is there a way for the createNewUser method to get the @RequestMapping value assigned to confirmUser? I'd like to use this value to generate the confirmation link instead of hard coding it.

// Process form input data
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult) {

}

// Process confirmation link
// Link in confirmation e-mail will be /registerConfirmation?token=UUID
@RequestMapping(value="/registerConfirmation", method = RequestMethod.GET)
public ModelAndView confirmUser( @RequestParam("token") String token) {

}
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I don't know of a way to get it from the @RequestMapping value but you have a couple of different options.

Option 1: Create a constant for the mapping and use that which allows you to reference it in both methods.

private final static String REGISTER_CONF_VAL = "/registerConfirmation";

@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult) {

}

// Process confirmation link
// Link in confirmation e-mail will be /registerConfirmation?token=UUID
@RequestMapping(value=REGISTER_CONF_VAL, method = RequestMethod.GET)
public ModelAndView confirmUser( @RequestParam("token") String token) {

}

Option 2: Less ideal, but if you add registerConfirmation to your config file, you can access it like:

@RequestMapping(value="${register.conf.val}", method = RequestMethod.GET)
public ModelAndView confirmUser( @RequestParam("token") String token) {

}

The reason this isn't ideal is because you probably don't want it to be different from environment to environment. That said, it would work.

about 4 years ago · Santiago Trujillo Relatório

0

If you need to generate the link based on user request, you can use the Path Variable in the controller. U can get the path variable and use some mechanism to validate the path as well.

Replace registerConfirmation with {registerConfirmation} and in the method, use @PathVariable annotation to get the path. Use the variable to check if the path is valid.

about 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