Spring Boot请求端点返回404


问题内容

该应用程序使用JDK 8,Spring Boot和Spring Boot Jersey启动程序,并打包为WAR(尽管它是通过Spring Boot
Maven插件在本地运行的)。

我想做的是获得我在运行中(在构建时)生成的文档作为欢迎页面。

我尝试了几种方法:

  1. 通过按照配置application.properties 适当的init参数,让Jersey提供静态内容
  2. 引入metadata-complete=false web.xml以便将生成的HTML文档列出为欢迎文件。

这些都没有解决。

我想避免不得不启用Spring MVC或创建仅用于提供静态文件的Jersey资源。

任何的想法?

这是Jersey的配置类(我尝试在那添加一个失败ServletProperties.FILTER_STATIC_CONTENT_REGEX):

@ApplicationPath("/")
@ExposedApplication
@Component
public class ResourceConfiguration extends ResourceConfig {

   public ResourceConfiguration() {
      packages("xxx.api");
      packages("xxx.config");
      property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);
      property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
   }
}

这是Spring
Boot应用程序类(我尝试添加application.propertieswith,spring.jersey.init.jersey.config.servlet.filter.staticContentRegex=/.*html但没有用,我不确定在这里应该使用什么属性键):

@SpringBootApplication
@ComponentScan
@Import(DataConfiguration.class)
public class Application extends SpringBootServletInitializer {

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

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

问题答案:

让我首先说一下,不能提供静态内容的原因是因为Jersey
servlet的默认servlet映射为/*,并且它占用了所有请求。因此,无法访问提供静态内容的默认servlet。除了以下解决方案之外,另一个解决方案是仅更改servlet映射。您可以通过用注释ResourceConfig子类@ApplicationPath("/another- mapping")或设置application.properties属性来实现spring.jersey.applicationPath


关于第一种方法,请看一下Jersey
ServletProperties。您尝试配置的属性是FILTER_STATIC_CONTENT_REGEX。它指出:

该属性仅在Jersey Servlet容器配置为作为过滤器运行时适用,否则将忽略此属性

spring开机默认配置了泽西servlet容器为一个Servlet(如提到这里):

默认情况下,Jersey将以名为@Bean的类型设置为Servlet 。您可以通过创建自己的同名豆之一来禁用或覆盖该bean。
您还可以通过设置使用过滤器而不是Servlet
(在这种情况下,替换或覆盖的是)。ServletRegistrationBean``jerseyServletRegistration
spring.jersey.type=filter@Bean``jerseyFilterRegistration

因此,只要设置属性spring.jersey.type=filter在你的application.properties,它应该工作。我已经测试过了

对于FYI,无论是配置为Servlet过滤器还是Servlet,就Jersey而言,功能都是相同的。

As an aside, rather then using the FILTER_STATIC_CONTENT_REGEX, where you
need to set up some complex regex to handle all static files, you can use the
FILTER_FORWARD_ON_404.
This is actually what I used to test. I just set it up in my ResourceConfig

@Component
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        packages("...");
        property(ServletProperties.FILTER_FORWARD_ON_404, true);
    }
}