How can I serve static html from spring boot?

前端 未结 6 913
感情败类
感情败类 2020-12-05 06:18

I ran the spring-boot-sample-web-static project from here, made this alteration to the pom


    org.springframework.boot&l         


        
6条回答
  •  猫巷女王i
    2020-12-05 07:04

    In Spring boot, /META-INF/resources/, /resources/, static/ and public/ directories are available to serve static contents.

    So you can create a static/ or public/ directory under resources/ directory and put your static contents there. And they will be accessible by: http://localhost:8080/your-file.ext. (assuming the server.port is 8080)

    You can customize these directories using spring.resources.static-locations in the application.properties.

    For example:

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

    Now you can use custom/ folder under resources/ to serve static files.

    Update:

    This is also possible using java config:

    @Configuration
    public class StaticConfig implements WebMvcConfigurer {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/static/**").addResourceLocations("classpath:/custom/");
        }
    }
    

    This confugration maps contents of custom directory to the http://localhost:8080/static/** url.

提交回复
热议问题