0

I would like to dynamically create the route for a redirect. I don't want to hardcode the route but would like to refer to the name of the route (route_index). This way I can change the route without having to change the redirect as well.

@Controller
public class IndexController {
    @GetMapping("/")
    public ModelAndView index() {
     public ModelAndView index(HttpServletRequest request) {
        String langCode = request.getLocale().getLanguage();
        return new ModelAndView("redirect:" + String.format("/%s/route", langCode));
    }
    }
}

@RequestMapping("/{lang}")
public class MyRouteController {
    @GetMapping(
            value = "/route",
            name = "route_index"
    )
    public @ResponseBody String index() {
        return "Index Route";
    }
}

I know the following from Symfony. Is something like this also possible in Spring Boot?

return $this->redirectToRoute(
    'route_index',
    [
        'lang' => 'en'
    ],
    301
);
Hans FooBar
  • 149
  • 11

1 Answers1

0

Please take a look at this. It might me what you're looking for.

Redirect to dynamic URL in Spring MVC

Vikingslord
  • 86
  • 10
  • I had already found this question and answers. But this is not what I would like. I want to avoid hard coding the route to other actions. I don't think Spring offers a solution for this. I think you have to build your own service with refelections that gets the data parameters from the annotations like in Symfony. – Hans FooBar May 16 '23 at 09:45