Spring Boot unabe用于提供资源文件夹

时间:2017-01-17 08:00:33

标签: spring spring-boot

在我的Spring Boot应用程序中,我有一个以下静态文件:

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

这是我的application.properties

server.port: 8080
server.contextPath: /api

我正在尝试通过以下网址访问此文件:

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

但服务器返回404 Not Found.

我做错了什么以及如何解决?

已更新

我的pom文件:

<?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>

8 个答案:

答案 0 :(得分:3)

@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {

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

我希望它会对你有所帮助!

也许您已添加@EnableWebMvc,它会查找与/api/images/social/facebook/f_logo.jpg对应的处理程序 只需通过提供类似于我的帖子的WebMvcConfigurer或WebMvcConfigurationSupport来删除它。

答案 1 :(得分:3)

我有同样的问题。 (春季启动2)

我在下面解决。

步骤1 :将images文件夹从src/main/resources/static/images移至src/main/webapp/WEB-INF/images

第2步:查找SpringBootMainApplication.java添加代码

@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());
}

步骤3 :运行程序并转到浏览器http://localhost:8080/images/logo.png

如果您设置了contextRoot http://localhost:8080/project/images/logo.png

完整代码

@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);
    }
}

为我工作。


其他

或者您可以在下面进行设置,如果要在资源文件夹中使用。

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

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

因为在下面部署了项目结构之后。

enter image description here

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

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

对不起,英语。

答案 2 :(得分:1)

在应用程序属性文件中设置以下行:

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

这是经过测试的。

答案 3 :(得分:1)

在使用apache tomcate 9.0运行spring boot应用程序时,我遇到了同样的问题。 rightnow解决了同样的问题。 1.我在我的应用程序中添加了validation.properties文件。文件夹结构如下所示,    --src / main / resources     - 静态的    --application.properties    --validations.properties 2.接下来,我将图像,css和js文件夹移到src / main / webapp文件夹下。 3.接下来,我已完成以下三个步骤,    -清理项目    --maven安装    -更新项目 4.终于运行了应用程序,它运行成功。

答案 4 :(得分:0)

经过一些研究和重现后,我可以确认您提供的资源路径和链接是否正确。

在你的pom.xml中,你有很多&#34;而不是Spring-boot相关的&#34;依赖。如果您想进一步使用此配置,则需要遵循gipple lake's answer,因为您没有使用spring-boot-web的自动配置。

但是如果你想开箱即用的解决方案,我建议用Spring-boot相关的方法替换Spring核心依赖项。

例如使用:

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
</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>

也许您还应该添加启动启动器依赖项:

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

答案 5 :(得分:0)

这是一个干净的解决方案,可以从resources文件夹提供服务。在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);
        }
    }

您可以使它成为Application的嵌套类(或您的任何扩展SpringBootServletInitializer的类)。您可以声明许多文件夹,例如

  

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

确保src / main / resources是源文件夹,并且您指定的文件夹位于该文件夹的正下方。

然后,您可以在html或jsp文件中简单地显示类似图像。

<img src="ok.png"/>

答案 6 :(得分:0)

只需将您的图像保留在您提供的路径中\src\main\resources\static\images\social\facebook\f_logo.jpg 并在 JSP 中加载图像时,像这样提供资源/静态的相对路径 src=" /images/social/facebook/f_logo.jpg”。这将加载图像。

答案 7 :(得分:-1)

您能告诉我们您的配置文件吗?

当网址未映射时,您获得了404,请向我们提供控制器。

您也可以查看:How to handle static content in Spring MVC?http://www.baeldung.com/spring-mvc-static-resources(教程)