Spring Boot:使用war packagin提供静态内容

fir*_*ren 10 spring spring-mvc spring-boot

我试图在spring boot中使用自动配置的服务静态内容.我读过它就足以将该内容放入/static//resources/配置@Controller以便执行此操作.

在我的项目中,它看起来根本不起作用.

我把它与它进行spring-boot-sample-web-ui了比较,我唯一不同的是包装.有jar和我有war包装.

你能否确认我在war包装中的spring-boot中这个自动配置的静态内容服务不起作用?

Pet*_*els -1

您需要定义一个 servletInitializer,而不是main方法(这是创建 web.xml 的替代方法)

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class MyServletInitializerextends extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}
Run Code Online (Sandbox Code Playgroud)

编辑:不要忘记将其添加到 componentScan 中