user3278795
user3278795

Reputation: 11

spring boot web application does not serve static content

I am working on a spring boot application using angular framework. I have structured the project as following :

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

Upvotes: 1

Views: 3637

Answers (2)

user2803064
user2803064

Reputation: 33

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 .

Upvotes: 1

Andy Wilkinson
Andy Wilkinson

Reputation: 116231

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.

Upvotes: 2

Related Questions