I am working on a spring boot application using angular framework. I have structured the project as following :
- src/main/java : spring-boot files, controllers, persistence
- src/main/resources : application.properties
- src/main/app : "webapp" files, angular, css, modules
In order to have spring serve static content under /app I've extended the class WebMvcConfigurerAdapter by overriding addResourceHandlers method :
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "com.myapp.controller" })
public class WebMvcConfig extends WebMvcConfigurerAdapter {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "/" }; //src/main/webapp works by default
private static final String[] RESOURCE_LOCATIONS = { "classpath:/src/main/app/", "classpath:/src/main/app/" };
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
if (!registry.hasMappingForPattern("/**")) {
registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS).setCachePeriod(0);
}
if (!registry.hasMappingForPattern("/app/**")) {
registry.addResourceHandler("/app/**").addResourceLocations(RESOURCE_LOCATIONS).setCachePeriod(0);
}
}}
When trying to access angular main file using
http://localhost:8080/app/app.html
I get an http error 404. For some reason if I rename /app folder into /webapp I am able to target http://localhost:8080/app.html . I'm definitely missing out something but can't figure out what it is . Thanks
src/main/appunder your app. Have you configured the Maven or Gradle to include that directory? Moreover, why not just follow the standard Spring Boot conventions for static content? - geoand