Springboot does not serve static content

▼魔方 西西 提交于 2021-01-29 10:27:14

问题


I am not able to access static content (angular app) or even a simple index.html file from spring-boot. I keep getting 404 error. Spring is not serving me with those static files. I have the problem since I have upgraded to Spring-Boot 2.2.4. I had to upgrade to counter Zip64 issue.

I have this line in my application.properties:

spring.resources.static-locations=classpath:/resources/,classpath:/static/

I also have my own staticResourceConfiguration class.

In my WebSecurity class, i have the following which is supposed to serve static content if the url path is matched to the below patterns. The problem is localhost:8080/index.html works and localhost:8080 return 404

        .antMatchers(HttpMethod.GET, "/**","/**/*.html", "/static/favicon.ico", "/**/*.js", "/**/*.js.map", "/**/*.css", "/**/*.png", "/**/*.jpg", "/**/*.jpeg", "/**/*.gif", "/**/*.ttf", "/**/*.json", "/**/*.woff", "/**/*.woff2", "/**/*.eot", "/**/*.svg","/**/*.json").permitAll()

回答1:


You can use this answer to improve your own
Spring Boot not serving static content

or this

@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("file:/path/to/staticPage/");
    }
}


来源:https://stackoverflow.com/questions/60119885/springboot-does-not-serve-static-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!