I have a simple frontend application made using Angular. All routings are handled by Angular so I want Spring boot to forward all page traffics to "/"
so angular routing can handle them.
There are already some known answers in stackoverflow. They are:
- spring-boot-single-page-application-routing-on-any-url
- configure-spring-boot-for-spa-frontend
- spring-boot-with-redirecting-with-single-page-angular2
But none of them work anymore because from Spring Framework 5.3 and onwards AntPathMatcher
has been replaced with PathPattern
. PathPattern
is compatible with AntPathMatcher
syntax except for the following:
Support for "**" for multi-segment matching is only allowed at the end of a pattern. This helps to eliminate most causes of ambiguity when choosing the closest match for a given request.
So this won't work anymore:
// Match everything without a suffix (so not a static resource)
@RequestMapping(value = "/**/{path:[^.]*}")
public String redirectAngularRoutes() {
return "forward:/";
}
So what is the best approach now starting with Spring Framework 5.3?
Do we specify each angular routing and forward
them to "/"
like below or there are other alternatives?
@RequestMapping({ "/help/**","/view/**","/admin/**" })
public String redirectAngularRoutes() {
return "forward:/";
}