Spring-Boot Jersey:允许 Jersey 提供静态内容 [英] Spring-Boot Jersey: allow Jersey to serve static content

查看:46
本文介绍了Spring-Boot Jersey:允许 Jersey 提供静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序使用 JDK 8、Spring Boot &Spring Boot Jersey 启动器并打包为 WAR(虽然它通过 Spring Boot Maven 插件在本地运行).

The application uses JDK 8, Spring Boot & Spring Boot Jersey starter and is packaged as a WAR (although it is locally run via Spring Boot Maven plugin).

我想做的是将我即时生成的文档(在构建时)作为欢迎页面.

What I would like to do is to get the documentation I generate on the fly (at build time) as a welcome page.

我尝试了几种方法:

  1. 通过在 application.properties 中进行配置,让 Jersey 为静态内容提供服务 正确的初始化参数,如这里
  2. 引入 metadata-complete=false web.xml 以将生成的 HTML 文档列为欢迎文件.
  1. letting Jersey serving the static contents by configuring in application.properties the proper init parameter as described here
  2. introduce a metadata-complete=false web.xml in order to list the generated HTML document as a welcome-file.

这些都没有奏效.

我想避免启用 Spring MVC 或仅为提供静态文件而创建 Jersey 资源.

I would like to avoid having to enable Spring MVC or creating a Jersey resource just for serving a static file.

有什么想法吗?

这是 Jersey 配置类(我尝试在其中添加 ServletProperties.FILTER_STATIC_CONTENT_REGEX 没有成功):

Here is the Jersey configuration class (I unsuccessfully tried to add a ServletProperties.FILTER_STATIC_CONTENT_REGEX there):

@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.propertiesspring.jersey.init.jersey.config.servlet.filter.staticContentRegex=/.*html 但它不起作用,我不确定这里的属性键应该是什么):

And here is Spring Boot application class (I tried adding an application.properties with spring.jersey.init.jersey.config.servlet.filter.staticContentRegex=/.*html but it didn't work, I'm not exactly sure what the property key should be here):

@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.

Let me just first state, that the reason the static content won't be served is because of the default servlet mapping of the Jersey servlet, which is /*, and hogs up all the requests. So the default servlet that serves the static content can't be reached. Beside the below solution, the other solution is to simply change the servlet mapping. You can do that by either annotating your ResourceConfig subclass with @ApplicationPath("/another-mapping") or set the application.properties property spring.jersey.applicationPath.

关于您的第一种方法,请查看 Jersey ServletProperties.您尝试配置的属性是 <代码>FILTER_STATIC_CONTENT_REGEX.它指出:

In regards to your first approach, take a look at the Jersey ServletProperties. The property you are trying to configure is FILTER_STATIC_CONTENT_REGEX. It states:

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

The property is only applicable when Jersey servlet container is configured to run as a Filter, otherwise this property will be ignored

默认情况下,Spring Boot 将 Jersey servlet 容器配置为 Servlet(如前所述 此处):

Spring Boot by default configures the Jersey servlet container as a Servlet (as mentioned here):

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

By default Jersey will be set up as a Servlet in a @Bean of type ServletRegistrationBean named jerseyServletRegistration. You can disable or override that bean by creating one of your own with the same name. You can also use a Filter instead of a Servlet by setting spring.jersey.type=filter (in which case the @Bean to replace or override is jerseyFilterRegistration).

所以只需在您的 application.properties 中设置属性 spring.jersey.type=filter,它应该可以工作.我已经测试过了.

So just set the property spring.jersey.type=filter in your application.properties, and it should work. I've tested this.

而FYI,无论是配置成Servlet Filter还是Servlet,就Jersey而言,功能都是一样的.

And FYI, whether configured as Servlet Filter or a Servlet, as far as Jersey is concerned, the functionality is the same.

顺便说一句,与其使用 FILTER_STATIC_CONTENT_REGEX(您需要设置一些复杂的正则表达式来处理所有静态文件),不如使用 FILTER_FORWARD_ON_404.这实际上是我用来测试的.我只是在我的 ResourceConfig

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);
    }
}

这篇关于Spring-Boot Jersey:允许 Jersey 提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆