Accessing static files outside of the application folder using Spring Boot 2.2

Viewed 319

In our application, we have to serve static files from a folder outside of the application folder. The location of the static file folder is /var/tmp/myapp/attachments additionally to the default locations. The application is implemented to create a deployable war (extending SpringBootServletInitializer.class). Using Spring Boot 2.2 and Tomcat 9

Here is the setup of my application:

1. application.yml file
    main:
        web-application-type: none

    # tried adding static resource location here but did not work
    #resources:
         #static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:///var/tmp/myapp/attachments/

2. Application.class
     @SpringBootApplication(
    // We are including Spring, but don't want it to stomp on the legacy servlet container, so disable various configurations.
    exclude = {
            DispatcherServletAutoConfiguration.class,
            ErrorMvcAutoConfiguration.class,
            WebMvcAutoConfiguration.class,
            MongoAutoConfiguration.class,
            MongoDataAutoConfiguration.class},
    scanBasePackages = {"com.app", "com.appmodule"})
 public class Application extends SpringBootServletInitializer {

   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
     return builder.sources(Application.class);
   }
 }

3. pom.xml
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

Tried Solutions: 1. Tried to add spring-resources-static-locations to application.yml 2. Tried to write:

          @Configuration
          public class StaticResourceConfiguration implements WebMvcConfigurer {
           @Override
           public void addResourceHandlers(ResourceHandlerRegistry registry) {
              registry.addResourceHandler("/myapp/**")
                      .addResourceLocations("classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:///var/tmp/myapp/attachments/");
       }
     }

The link created to access static locations: 1. http://my.development.com:8080/myapp/attachments/6b8f6b78-e0e3-4163-89df-2861ac58bc65/common/images/COVER_PANE_FOOTER_COMPANY_LOGO/11/CompanyLogo.jpg 2. also tried: http://my.development.com:8080/attachments/6b8f6b78-e0e3-4163-89df-2861ac58bc65/common/images/COVER_PANE_FOOTER_COMPANY_LOGO/11/CompanyLogo.jpg

Could you please help me figure out based on current application configuration what is the best way to achieve it?

Thank you!

0 Answers
Related