该应用程序使用 JDK 8、Spring Boot 和 Spring Boot Jersey 启动器,并打包为 WAR(尽管它是通过 Spring Boot Maven 插件在本地运行)。
我想做的是获取我动态(在构建时)生成的文档作为欢迎页面。
我尝试了几种方法:
application.properties
正确的初始化参数中配置来提供静态内容,如此处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.properties
和 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
来实现此目的。
ServletProperties
。您尝试配置的属性是 FILTER_STATIC_CONTENT_REGEX
。它指出:
该属性仅在 Jersey servlet 容器配置为作为 Filter 运行时适用,否则该属性将被忽略
Spring Boot 默认将 Jersey Servlet 容器配置为 Servlet(如此处所述):
默认情况下,Jersey 将被设置为
类型的@Bean
中名为ServletRegistrationBean
的 Servlet。您可以通过创建您自己的同名 bean 来禁用或覆盖该 bean。 您还可以通过设置jerseyServletRegistration
来使用过滤器而不是 Servlet(在这种情况下,要替换或覆盖的spring.jersey.type=filter
是@Bean
)。jerseyFilterRegistration
因此只需在
spring.jersey.type=filter
中设置属性 application.properties
即可,它应该可以工作。我已经测试过这个。
仅供参考,无论配置为 Servlet Filter 还是 Servlet,就 Jersey 而言,功能都是相同的。
顺便说一句,您可以使用
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
配置文件。
一旦我删除了上述方法,并将
@Configuration
注释放在我的 ResourceConfig
类上,我注意到可以通过 Spring Boot 检索静态资源。
为了完整起见,这是我现在的
ResourceConfig
的片段。
@Configuration
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
// Application specific settings
property(ServletProperties.FILTER_FORWARD_ON_404, true);
}
}
这篇博文有助于确定
ResourceConfig
的差异方法。
以下设置对我有用
设置
spring .jersey.type: filter
设置 FILTER_FORWARD_ON_404
@Configuration
public class MyResourceConfig extends ResourceConfig {
public MyResourceConfig () {
try {
register(XXX.class);
property(ServletProperties.FILTER_FORWARD_ON_404, true);
} catch (Exception e) {
LOGGER.error("Exception: ", e);
}
}
}
注意:使用 @Configuration 而不是 @component
在我们的例子中,我们的项目完全基于 jersey JAX-RS 构建,因此我们不能将其用作过滤器,因为它会破坏整个项目。所以,这就是我们所做的。
将
spring-boot-starter-web
添加到您的依赖项中,将 spring MVC 添加到您的类路径中。由于 Spring Actuator 是基于 MVC 构建的,因此您需要更改上下文路径。
server.servlet.context-path=/
spring.mvc.servlet.path=/management
spring.jersey.application-path=/
/
/management
,以便执行器端点变为 /management/actuator/health
/
,以使其他路线按原样工作参考 - https://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/html/howto-actuator.html