Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Spring Framework 5.3+, Spring Boot 2.3+ Routing for SPA frontend

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:/";
  }
like image 557
Imtiaz Shakil Siddique Avatar asked Dec 01 '25 02:12

Imtiaz Shakil Siddique


1 Answers

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.

like image 192
jkrshw Avatar answered Dec 02 '25 16:12

jkrshw