19

In my Spring Boot application I have a following static file:

\src\main\resources\static\images\social\facebook\f_logo.jpg

This is my application.properties

server.port: 8080
server.contextPath: /api

I'm trying to access this file by the following url:

http://localhost:8080/api/images/social/facebook/f_logo.jpg

but the server returns 404 Not Found.

What am I doing wrong and how to fix it ?

UPDATED

My pom files:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>example</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>

    <name>example</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jackson.version>2.8.0</jackson.version>
        <spring.version>4.3.5.RELEASE</spring.version>
        <spring.boot.version>1.4.3.RELEASE</spring.boot.version>
        <cdi-api.version>2.0-EDR1</cdi-api.version>
        <slf4j.version>1.7.18</slf4j.version>
        <logback.version>1.1.6</logback.version>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <commons-lang3.version>3.4</commons-lang3.version>
        <commons-validator.version>1.5.0</commons-validator.version>
        <commons-io.version>2.4</commons-io.version>

        <jacoco-maven-plugin.version>0.7.4.201502262128</jacoco-maven-plugin.version>
        <maven-surefire-plugin.version>2.19</maven-surefire-plugin.version>
        <lifecycle-mapping.version>1.0.0</lifecycle-mapping.version>

        <java.source.version>1.8</java.source.version>
        <java.target.version>1.8</java.target.version>
    </properties>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>neo4j-release-repository</id>
            <name>Neo4j Maven 2 release repository</name>
            <url>http://m2.neo4j.org/content/repositories/releases</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-libs-milestone</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-maven-plugin.version}</version>
                <configuration>
                    <excludes>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>jacoco.agent.argLine</propertyName>
                            <destFile>${jaCoCoExecutionDataFile}</destFile>
                        </configuration>
                    </execution>
                    <execution>
                        <id>jacoco-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <argLine>${jacoco.agent.argLine}</argLine>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings 
                    only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>${lifecycle-mapping.version}</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.jacoco</groupId>
                                        <artifactId>
                                            jacoco-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [${jacoco-maven-plugin.version},)
                                        </versionRange>
                                        <goals>
                                            <goal>prepare-agent</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>

        <!-- Jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring.boot.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>${cdi-api.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>${commons-validator.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>

        <!-- jUnit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <modules>
        <module>domain</module>
        <module>api</module>
        <module>ui</module>
    </modules>
</project>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>example</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>domain</artifactId>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <neo4j.version>3.1.0</neo4j.version>
        <lucene.version>5.5.0</lucene.version>
        <spring-data-neo4j.version>4.2.0.BUILD-SNAPSHOT</spring-data-neo4j.version>
        <neo4j-ogm.version>2.1.1-SNAPSHOT</neo4j-ogm.version>
        <spring-social-security.version>1.1.4.RELEASE</spring-social-security.version>
        <spring-security-oauth2.version>2.0.11.RELEASE</spring-security-oauth2.version>
        <hibernate-validator.version>5.2.4.Final</hibernate-validator.version>
    </properties>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>neo4j-snapshot-repository</id>
            <name>Neo4j Maven 2 snapshot repository</name>
            <url>http://m2.neo4j.org/content/repositories/snapshots</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>${java.source.version}</source>
                    <target>${java.target.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <optimize>true</optimize>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <!-- neo4j -->
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-core</artifactId>
            <version>${neo4j-ogm.version}</version>
        </dependency>

        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-http-driver</artifactId>
            <version>${neo4j-ogm.version}</version>
        </dependency>

        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-embedded-driver</artifactId>
            <version>${neo4j-ogm.version}</version>
        </dependency>

        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-bolt-driver</artifactId>
            <version>${neo4j-ogm.version}</version>
        </dependency>

        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-enterprise</artifactId>
            <version>${neo4j.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.neo4j</groupId>
                    <artifactId>neo4j-security-enterprise</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j</artifactId>
            <version>${neo4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
            <version>${spring-data-neo4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-security</artifactId>
            <version>${spring-social-security.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>${spring-security-oauth2.version}</version>
        </dependency>


        <!-- Lucene -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>5.5.0</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernate-validator.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-social-twitter</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0-rc1</version>
        </dependency>

    </dependencies>

</project>

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <parent>
        <artifactId>example</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>api</artifactId>
    <packaging>war</packaging>

    <properties>
        <!-- example -->
        <com.example.domain.version>0.0.1</com.example.domain.version>
        <!-- 3rd party -->
        <spring-security-core.version>4.1.3.RELEASE</spring-security-core.version>
        <spring.social.google.version>1.0.0.RELEASE</spring.social.google.version>
        <spring-social-github.version>1.0.0.M4</spring-social-github.version>
        <javax.servlet-api.version>3.1.0</javax.servlet-api.version>
        <javaee-web-api.version>7.0</javaee-web-api.version>
        <json-path.version>2.0.0</json-path.version>
        <rest-assured.version>2.8.0</rest-assured.version>
        <httpclient>4.5.2</httpclient>
        <springfox-swagger2.version>2.6.0</springfox-swagger2.version>
        <gson.version>2.6.2</gson.version>
        <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
        <maven-war-plugin.version>2.6</maven-war-plugin.version>
        <java.source.version>1.8</java.source.version>
        <java.target.version>1.8</java.target.version>
    </properties>

    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
        </profile>
    </profiles>

    <dependencies>
        <!-- example -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>domain</artifactId>
            <version>${com.example.domain.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring-security-core.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-jwt</artifactId>
            <version>1.0.5.RELEASE</version>
        </dependency>

        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>${spring.boot.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <!-- Spring Boot Social Providers -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-social-facebook</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-social-linkedin</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <!-- Spring Social Providers -->
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-google</artifactId>
            <version>${spring.social.google.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-github</artifactId>
            <version>${spring-social-github.version}</version>
        </dependency>

        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>${json-path.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path-assert</artifactId>
            <version>${json-path.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- default j2ee dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${javax.servlet-api.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>${javaee-web-api.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>${httpclient}</version>
        </dependency>

        <!-- Swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox-swagger2.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox-swagger2.version}</version>
        </dependency>

        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>${rest-assured.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${gson.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.12</version>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>${project.basedir}/src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <addResources>false</addResources>
                    <jvmArguments>
                        -Xdebug
                        -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
                    </jvmArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <version>${maven-compiler-plugin.version}</version>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.source.version}</source>
                    <target>${java.target.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>${maven-war-plugin.version}</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
5
  • Just reproduce exact the same in a simple project and I am able to see my image. Maybe any typo? If not you have to share more infos. Commented Jan 17, 2017 at 8:16
  • Not a typo because it is working with a solution provided by @gipple lake below Commented Jan 17, 2017 at 8:18
  • OK. Can you provide your pom. Commented Jan 17, 2017 at 8:23
  • I think the problem is that you use Spring-web and spring-webmvc dependency and not the spring boot related one. What is if you use <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> Commented Jan 17, 2017 at 8:44
  • I have completely removed Spring-web and spring-webmvc but the issue is not gone Commented Jan 17, 2017 at 8:58

9 Answers 9

23
@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){ 
            registry.addResourceHandler("/**")
                 .addResourceLocations("classpath:/static/");
    }
}

I hope this helps you!

Maybe you have added @EnableWebMvc which looks for handlers corresponding to /api/images/social/facebook/f_logo.jpg. Just remove that & provide WebMvcConfigurer or WebMvcConfigurationSupport similar to what I have posted.

Sign up to request clarification or add additional context in comments.

6 Comments

its a spring boot app. Normally there is no need to configure resourcehandlers manually
Thanks, It is working ! But I agree with @Patrick - this is a Spring Boot and AFAIK - it should work by default. What am I doing wrong ?
Adding this configuration file works, but i've got a question. One of my images could be found without it but all others need this particular configuration. Can someone helps understand why !
How is anyone seeing this answer supposed to know which file to put this code in? Or how to get the framework to pick up that file? Come on.
It's public java class so you don't have too much choice but to put in in file WebConfiguration.java. Framework will pick it up due to @Configuration annotation
|
13

Same problem with me. (Spring Boot 2)

I resolve below.

Step 1: Move images folder from src/main/resources/static/images to src/main/webapp/WEB-INF/images

Step 2: Look up SpringBootMainApplication.java add code

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    // Register resource handler for images
    registry.addResourceHandler("/images/**").addResourceLocations("/WEB-INF/images/")
            .setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic());
}

Step 3: Run program and go to browser http://localhost:8080/images/logo.png

If your set contextRoot http://localhost:8080/project/images/logo.png

Full Code:

@SpringBootApplication
public class MainApplication implements WebMvcConfigurer {

    private static Logger logger = LoggerFactory.getLogger(MainApplication.class.getName());

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        // Register resource handler for images
        registry.addResourceHandler("/images/**").addResourceLocations("/WEB-INF/images/")
                .setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic());
    }

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

Work for me.


Additional

Or you can set below, If you want using in resources folder.

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/statics/")
            .setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic());
}

Because after deploy project structure below.

enter image description here

http://localhost:8080/images/logo.png

or

http://localhost:8080/project/images/logo.png

Sorry for English.

1 Comment

You shouldn't move resources out of recources. And there's no Web-Inf
7

Here's a clean solution to serve from resources folder. Tested on Spring Boot v2.1.4.RELEASE. enter image description here

    @Configuration
    public class WebConfig implements WebMvcConfigurer {      
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/static/","classpath:/image/")
            .setCachePeriod(0);
        }
    }

You can make this a nested class of Application (or whichever class of yours extends SpringBootServletInitializer). You can declare many folders like

.addResourceLocations("classpath:/static/","classpath:/image/","classpath:/largeimage/","classpath:/pdf/")

Make sure src/main/resources is a source folder, and the folders you specify are directly under it.

Then you can simply display images like this in your html or jsp files.

<img src="ok.png"/>

1 Comment

You can build an excellent and maintable UI using Java/HTML/CSS/JS. Tools like React add to many unhelpful abstractions and overcomplicate the front end. Don't be afraid to use SpringBoot for your UI!
6

in your application properties file set the following line :

spring.resources.static-locations=file:///c:/{yourWorkSpace}/{projectName}/src/main/resources/static/

this is tested.

1 Comment

Worked thank you. Do you know why we need to do this? Is it maybe because we did the setup incorrectly or is it a problem with spring boot not being able to see the path?
1

Just keep your images in path which you have given \src\main\resources\static\images\social\facebook\f_logo.jpg and while loading images in JSP, give relative path to resources/static like this src="/images/social/facebook/f_logo.jpg". This will load the image.

Comments

0

Instead of http://localhost:8080/api/images/social/facebook/f_logo.jpg,

try this http://localhost:8080/images/social/facebook/f_logo.jpg

It should work as the default path starts from /, not /api

Comments

-1

After some research and reproduce I can confirm that your provided resource path and your link is correct.

In your pom.xml you have many "not Spring-boot related" dependencies. If you want to go further with this configuration, you need to follow gipple lake's answer because you are not using the autoconfiguration of spring-boot-web.

But if you want to get to out of the box solution I would suggest to replace the Spring core dependencies with the Spring-boot related ones.

For example use:

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
</dependency>

instead of:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>

Maybe you should also add the boot starter dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

3 Comments

Adding a bunch of dependencies is not an answer to the question.
@AndrewKoster have you even read the full answer? Seems that you dont understand that this is a suggestion to switch to Spring-boot components to use an out of the box solution. Please make sure, that your comments are helpful in future
You're suggesting that people swap out their dependencies, without explaining: 1) why 2) how this affects the code or 3) how this affects the behavior of the web app. It's a very confusing answer, no matter how many times I re-read it.
-1

I have faced same issue while running spring boot application using apache tomcate 9.0. rightnow resolved the same. 1. i have added validation.properties file in my app. folder structure looking like below, --src/main/resources --static --application.properties --validations.properties 2. next i have moves images,css and js folder under src/main/webapp folder. 3. next i have done below three steps, --cleaned project --maven install --update project 4. finally ran application ,it was working successfully.

1 Comment

what? also, what
-2

Can you show us your configuration files?

You got 404 when the url is not mapped, please provide us the controller.

Also you might have a look at: How to handle static content in Spring MVC? and http://www.baeldung.com/spring-mvc-static-resources (tutorial)

5 Comments

getting down vote because asking for code, so i can hep you, thank you
You shared links which are not spring-boot related. If you use spring-boot you should NOT do the stuff you provided in the links.
so in my linked tutorial you could also find gipple answer, is MVC configuration related to spring boot, no? but does it work? seems it is, nevermind, this question got an answer, have a nice day
Did you even read the articles that you posted? They say things like "Serving static files to the client can be done in a variety of ways, and using a Spring Controller isn't necessarily the best available option. However, sometimes the controller route is necessary – and that's what we're going to be focused on in this quick article."
Obviously, in any sane web development environment, you shouldn't need to write an entire controller just to serve one static file. That's insane and not a practical solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.