首先我要声明,静态内容不会被提供的原因是 Jersey servlet 的默认 servlet 映射,即/*
,并占用所有请求。因此无法访问提供静态内容的默认 servlet。除了下面的解决方案之外,另一个解决方案是简单地更改 servlet 映射。您可以通过注释您的ResourceConfig
子类与@ApplicationPath("/another-mapping")
或设置application.properties
财产spring.jersey.applicationPath
.
关于您的第一种方法,请看一下球衣ServletProperties https://jersey.github.io/apidocs/2.26/jersey/org/glassfish/jersey/servlet/ServletProperties.html。您尝试配置的属性是FILTER_STATIC_CONTENT_REGEX https://jersey.github.io/apidocs/2.26/jersey/org/glassfish/jersey/servlet/ServletProperties.html#FILTER_STATIC_CONTENT_REGEX。它指出:
该属性仅在 Jersey servlet 容器配置为作为 Filter 运行时适用,否则该属性将被忽略
Spring Boot 默认将 Jersey servlet 容器配置为 Servlet(如前所述here http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-jersey):
默认情况下,Jersey 将被设置为 Servlet@Bean
类型的ServletRegistrationBean
named jerseyServletRegistration
。您可以通过创建您自己的同名 bean 来禁用或覆盖该 bean。您还可以通过设置使用 Filter 而不是 Servletspring.jersey.type=filter
(在这种情况下@Bean
替换或覆盖是jerseyFilterRegistration
).
所以只需设置属性spring.jersey.type=filter
在你的application.properties
,它应该可以工作。我已经测试过这个。
仅供参考,无论配置为 Servlet Filter 还是 Servlet,就 Jersey 而言,功能都是相同的。
顺便说一句,而不是使用FILTER_STATIC_CONTENT_REGEX
,当您需要设置一些复杂的正则表达式来处理所有静态文件时,您可以使用FILTER_FORWARD_ON_404 https://jersey.github.io/apidocs/2.26/jersey/org/glassfish/jersey/servlet/ServletProperties.html#FILTER_FORWARD_ON_404。这实际上是我用来测试的。我刚刚将其设置在我的ResourceConfig
@Component
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
packages("...");
property(ServletProperties.FILTER_FORWARD_ON_404, true);
}
}