How can I use Angular2 PathLocationStrategy in a Spring Boot application?

In our application we want to deploy an angular2 frontend into a spring boot backend ( into src/main/resources/static ) but get rid of the #’s in the urls and use the angular2 default PathLocationStrategy.

Leave a Reply

2 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

Just wanted to share how I did it, hope it helps somebody: (SpringBoot 1.3.3, Angular2.beta15, angular2-seed) import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.embedded.ErrorPage; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpStatus; @SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) {, args); } @Bean public EmbeddedServletContainerCustomizer containerCustomizer(){ return new Angular2PathLocationStrategyCustomizer(); } private static class Angular2PathLocationStrategyCustomizer implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container){ container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/")); } } }


These are the three steps you need to follow: Implement your own TomcatEmbeddedServletContainerFactory bean and set up the RewriteValve import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; ... import org.apache.catalina.valves.rewrite.RewriteValve; ... @Bean TomcatEmbeddedServletContainerFactory servletContainerFactory() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); factory.setPort(8080); factory.addContextValves(new RewriteValve()); return factory; } Add a rewrite.conf file to the WEB-INF directory of your application and specify the rewrite rules. Here is an example rewrite.conf content, which I’m using in the angular application to take advantage of the angular’s PathLocationStrategy (basicly I just redirect everything to the index.html as we just use spring boot to serve the static web content): RewriteCond %{REQUEST_URI} !^.*.(bmp|css|gif|htc|html?|ico|jpe?g|js|pdf|png|swf|txt|xml|svg|eot|woff|woff2|ttf|map)$ RewriteRule… Read more »