Spring-Boot Jersey:允许泽西岛提供静态内容
该应用程序使用JDK 8,Spring Boot和Spring Boot Jersey启动程序,并打包为WAR(尽管它是通过Spring Boot Maven插件在本地运行的)。
我想要做的是将我生成的文档(在构建时)生成为欢迎页面。
我尝试了几种方法:
- 让Jersey为静态内容提供服务,方法是在
application.properties
配置正确的init参数,如下所述 - 引入
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应用程序类(我尝试使用spring.jersey.init.jersey.config.servlet.filter.staticContentRegex=/.*html
添加一个application.properties
但它没有用,我不确定是什么属性键应该在这里):
@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映射。 您可以通过使用@ApplicationPath("/another-mapping")
注释ResourceConfig
子类或设置application.properties
属性spring.jersey.applicationPath
。
关于第一种方法,请查看Jersey ServletProperties
。 您尝试配置的属性是FILTER_STATIC_CONTENT_REGEX
。 它指出:
该属性仅在Jersey servlet容器配置为作为Filter运行时适用,否则将忽略此属性
Spring Boot默认将Jersey servlet容器配置为Servlet(如此处所述):
默认情况下,Jersey将在名为
jerseyServletRegistration
ServletRegistrationBean
类型的@Bean
中设置为Servlet。 您可以通过创建具有相同名称的bean来禁用或覆盖该bean。 您还可以通过设置spring.jersey.type=filter
来使用Filter而不是Servlet (在这种情况下,要替换或覆盖的jerseyFilterRegistration
是jerseyFilterRegistration
)。
所以只需在application.properties
设置属性spring.jersey.type=filter
,它应该可以工作。 我测试了这个。
而且,无论是配置为Servletfilter还是Servlet,就Jersey而言,其function都是相同的。
FILTER_FORWARD_ON_404
,而不是使用FILTER_STATIC_CONTENT_REGEX
,您需要设置一些复杂的正则表达式来处理所有静态文件,您可以使用FILTER_FORWARD_ON_404
。 这实际上是我以前测试的。 我只是在我的ResourceConfig
设置它
@Component public class JerseyConfig extends ResourceConfig { public JerseyConfig() { packages("..."); property(ServletProperties.FILTER_FORWARD_ON_404, true); } }
对于仍然无法使用它的人,我按照@peeskillet提供的答案,并且必须进行额外的更改。
以前我在Application.java
创建了以下方法。
@Bean public ServletRegistrationBean jerseyServlet() { ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/*"); registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName()); return registration; }
问题是这会为/*
路径注册servlet,然后设置Jersey ResourceConfig
配置文件。
一旦我删除了上面的方法,并在我的ResourceConfig
类上放置了@Configuration
注释,我注意到可以通过Spring Boot检索静态资源。
为了完整性,这是我的ResourceConfig
的片段。
@Configuration public class JerseyConfig extends ResourceConfig { public JerseyConfig() { // Application specific settings property(ServletProperties.FILTER_FORWARD_ON_404, true); } }
这篇博客文章有助于确定ResourceConfig
的差异方法。