1
votes

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

2
You have src/main/app under 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
I suggest a read of the spring boot reference guide (especially the ones that are already mapped by default) and the use of webjars. Basically what do in your WebMvcConfig class is already done by Spring Boot so you are just complicating things. - M. Deinum
@geoand Yes, I added the source directory 'src/main/app' to maven using build-helper-maven-plugin. Using standard src/main/static and commenting WebMvcConfig class I still have the same issue. - user3278795
@M.Deinum , indeed there are some default resource handlers (/public, /static) done by spring boot , but to me it makes more sense to have the angular files inside src/main/app or src/main/webapp - user3278795
You are right guys, but maybe spring boot is too rigid in that case...I would prefer to have my angular files inside /app instead of /public - user3278795

2 Answers

2
votes

You need to provide the resources' location on the classpath, not their location in your source. For example, resources in src/main/resources/static are served by the resource location classpath:/static/ as the Maven build results in everything in src/main/resources being at the root of the classpath. In short, a resource location of classpath:/src/main/app is almost certainly wrong: you need to configure Spring Boot to load resources from wherever you've configured Maven to build and package them.

1
votes

Add this file. It worked for me. suppose the static resource files were under the folder: src/main/webapp.

@Configuration public class WebMappingConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // 1. when put angularjs under src/main/resources/static.
    // String[] myExternalFilePath = {"classpath:/static/app/build/"};
    // 2. when put angularjs under src/main/webapp.
    String[] staticResourceMappingPath = { "/app/build/",
            "classpath:/static/", "classpath:/static/app/build/" };

    registry.addResourceHandler("/**").addResourceLocations(
            staticResourceMappingPath);
}

}

Try it: 1. cd /src/main/webapp 2. git clone https://github.com/ngbp/ngbp.git app 3. cd app 4. npm install
5. bower install 6. grunt watch

But when installing, the static resource won't be copied into the *.jar file, you need to copy all those files under the folder of app/build/ to src/main/resource/static .