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
);