当前位置:  开发笔记 > 编程语言 > 正文

Spring Boot无法从资源文件夹中提供静态图像

如何解决《SpringBoot无法从资源文件夹中提供静态图像》经验,为你挑选了1个好方法。

在我的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文件:



    4.0.0

    com.example
    example
    0.0.1
    pom

    example

    
        UTF-8
        2.8.0
        4.3.5.RELEASE
        1.4.3.RELEASE
        2.0-EDR1
        1.7.18
        1.1.6
        4.12
        1.2.17
        3.4
        1.5.0
        2.4

        0.7.4.201502262128
        2.19
        1.0.0

        1.8
        1.8
    

    
        
            spring-milestones
            Spring Milestones
            http://repo.spring.io/milestone
            
                false
            
        
        
            neo4j-release-repository
            Neo4j Maven 2 release repository
            http://m2.neo4j.org/content/repositories/releases
            
                true
            
            
                false
            
        
        
            spring-libs-milestone
            Spring Milestones
            https://repo.spring.io/libs-milestone
            
                false
            
        
    

    
        
            
                org.jacoco
                jacoco-maven-plugin
                ${jacoco-maven-plugin.version}
                
                    
                    
                
                
                    
                        jacoco-initialize
                        initialize
                        
                            prepare-agent
                        
                        
                            jacoco.agent.argLine
                            ${jaCoCoExecutionDataFile}
                        
                    
                    
                        jacoco-report
                        test
                        
                            report
                        
                    
                
            
            
                maven-surefire-plugin
                ${maven-surefire-plugin.version}
                
                    ${jacoco.agent.argLine}
                    -Dfile.encoding=UTF-8
                
            
        
        
            
                
                
                    org.eclipse.m2e
                    lifecycle-mapping
                    ${lifecycle-mapping.version}
                    
                        
                            
                                
                                    
                                        org.jacoco
                                        
                                            jacoco-maven-plugin
                                        
                                        
                                            [${jacoco-maven-plugin.version},)
                                        
                                        
                                            prepare-agent
                                        
                                    
                                    
                                        
                                    
                                
                            
                        
                    
                
            
        
    

    

        
        
            com.fasterxml.jackson.core
            jackson-core
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-databind
            ${jackson.version}
        

        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-expression
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        

        
        
            org.springframework.boot
            spring-boot
            ${spring.boot.version}
        
        
            org.springframework.boot
            spring-boot-autoconfigure
            ${spring.boot.version}
        
        
            org.springframework.boot
            spring-boot-starter-logging
            ${spring.boot.version}
        
        
            org.springframework.boot
            spring-boot-starter-test
            ${spring.boot.version}
            test
        

        
            javax.enterprise
            cdi-api
            ${cdi-api.version}
        

        
            javax.transaction
            jta
            1.1
        

        
            org.apache.commons
            commons-lang3
            ${commons-lang3.version}
        
        
            commons-validator
            commons-validator
            ${commons-validator.version}
        
        
            commons-io
            commons-io
            ${commons-io.version}
        

        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
            ch.qos.logback
            logback-classic
            ${logback.version}
        

        
        
            junit
            junit
            ${junit.version}
            test
        

    

    
        domain
        api
        ui
    




    
        example
        com.example
        0.0.1
    

    4.0.0

    domain
    jar

    
        UTF-8
        UTF-8

        3.1.0
        5.5.0
        4.2.0.BUILD-SNAPSHOT
        2.1.1-SNAPSHOT
        1.1.4.RELEASE
        2.0.11.RELEASE
        5.2.4.Final
    

    
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/libs-snapshot
            
                true
            
        
        
            neo4j-snapshot-repository
            Neo4j Maven 2 snapshot repository
            http://m2.neo4j.org/content/repositories/snapshots
            
                true
            
            
                true
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.5.1
                
                    ${java.source.version}
                    ${java.target.version}
                    ${project.build.sourceEncoding}
                    true
                
            
            
                org.apache.maven.plugins
                maven-resources-plugin
                2.6
                
                    ${project.build.sourceEncoding}
                
            
        
    

    

        
        
            org.neo4j
            neo4j-ogm-core
            ${neo4j-ogm.version}
        

        
            org.neo4j
            neo4j-ogm-http-driver
            ${neo4j-ogm.version}
        

        
            org.neo4j
            neo4j-ogm-embedded-driver
            ${neo4j-ogm.version}
        

        
            org.neo4j
            neo4j-ogm-bolt-driver
            ${neo4j-ogm.version}
        

        
            org.neo4j
            neo4j-enterprise
            ${neo4j.version}
            
                
                    org.neo4j
                    neo4j-security-enterprise
                
            
        
        
            org.neo4j
            neo4j
            ${neo4j.version}
        

        
            org.springframework.data
            spring-data-neo4j
            ${spring-data-neo4j.version}
        

        
            org.springframework.security
            spring-security-web
            4.1.3.RELEASE
        

        
            org.springframework.social
            spring-social-security
            ${spring-social-security.version}
        

        
            org.springframework.security.oauth
            spring-security-oauth2
            ${spring-security-oauth2.version}
        &l

小智.. 6

@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> 小智..:
@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即可将其删除。

推荐阅读
放ch养奶牛
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有