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

查看:177
本文介绍了Spring-Boot 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 正确的init参数 as描述此处

  2. 介绍 metadata-complete = false web.xml ,以便将生成的HTML文档列为欢迎-file。

  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.properties ,带有 spring.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 property 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容器配置为作为Filter运行时适用,否则将忽略此属性

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将在 @Bean 中设置为Servlet,类型为 ServletRegistrationBean 命名 jerseyServletRegistration 。您可以通过创建具有相同名称的bean来禁用或覆盖该bean。 你也可以通过设置 spring.jersey.type = filter 来使用Filter而不是Servlet(在这种情况下要替换或覆盖的@Bean 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过滤器还是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:允许泽西岛提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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