Avoid @EnableWebMvc
WebConfig
package net.pupli.web_server.config; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpStatus; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.GsonHttpMessageConverter; import org.springframework.web.servlet.config.annotation.*; import org.springframework.web.servlet.resource.EncodedResourceResolver; import org.springframework.web.servlet.resource.GzipResourceResolver; import org.springframework.web.servlet.resource.PathResourceResolver; import java.io.IOException; import java.util.List; @Configuration @EnableAutoConfiguration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // enable cache for static files /* registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/") .setCachePeriod(3600 * 24 * 7);*/ // serve pre gzip files in static folder registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/") .setCachePeriod(3600 * 24 * 7) .resourceChain(false) .addResolver(new EncodedResourceResolver()); registry.addResourceHandler("/index.html") .addResourceLocations("classpath:/static/") .setCachePeriod(0); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/notFound").setViewName("forward:/index.html"); } @Bean public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> containerCustomizer() { return container -> { container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notFound")); }; } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter(); converters.add(gsonHttpMessageConverter); } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); } }
References
https://stackoverflow.com/questions/44692781/configure-spring-boot-to-redirect-404-to-a-single-page-app
https://stackoverflow.com/questions/39331929/spring-catch-all-route-for-index-html/42998817