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:
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:/";
}
You can configure the path matching strategy to use the ant path matcher in your application.yml
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
There is currently no deprecation warning on the Ant Path Matcher but it seems possible it could be removed in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With