Spring 当包含 Jersey REST 时启动不提供静态内容

Spring boot not serving static content when Jersey REST is included

我在尝试从 spring 启动应用程序提供 index.html(位于 main/resources/static 下)时收到 HTTP 404 错误。但是,如果我从项目中删除基于 Jersey 的 JAX-RS class,则 http://localhost:8080/index.html 可以正常工作。

以下为主要class

@SpringBootApplication
public class BootWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootWebApplication.class, args);
    }
}

我不确定我是否遗漏了什么。

谢谢

问题在于 Jersey servlet 路径的默认设置,默认设置为 /*。这占用了所有请求,包括对静态内容的默认 servlet 的请求。所以请求去 Jersey 寻找静态内容,当它在 Jersey 应用程序中找不到资源时,它会发出 404。

你有几个选择:

  1. 将 Jerse 运行时配置为过滤器(而不是默认情况下作为 servlet)。请参阅 了解如何做到这一点。同样使用此选项,您需要配置 ServletProperties 之一以将 404 转发到 servlet 容器。您可以使用配置 Jersey 的 属性 转发 all 请求,这会导致找不到 Jersey 资源,或者使用 属性 允许您配置正则表达式转发请求的模式。

  2. 您只需将 Jersey servlet 模式更改为默认模式以外的其他模式即可。最简单的方法是用 @ApplicationPath("/root-path") 注释 ResourceConfig 子类。或者您可以在 application.properties - spring.jersey.applicationPath.

  3. 中配置它