【问题标题】:Spring boot metrics with Jersey2Jersey2 的 Spring Boot 指标
【发布时间】:2016-03-07 16:07:25
【问题描述】:

我有一个运行 spring-boot、jersey2 和 spring 指标的应用程序: 下面是maven sn-p:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在引入执行器依赖之前,Jersey 一直运行良好。 然后创建了以下 bean 以使 Jersey 用作过滤器:

@Bean
public FilterRegistrationBean jerseyFilterRegistration() {
    FilterRegistrationBean bean = new FilterRegistrationBean();
    bean.setName("jerseyFilter");
    bean.setFilter(new ServletContainer(resourceConfig()));
    bean.setOrder(Ordered.LOWEST_PRECEDENCE);
    bean.addInitParameter("com.sun.jersey.config.property.WebPageContentRegex", managementContextRegex);

    return bean;
}

指标映射到 /admin 路径。使用此配置,我无法使指标正常工作。但是,通过添加 management.port(不同于主应用程序端口),Jersey 资源和指标都可用。 我在这里缺少什么让指标和泽西资源开始在同一个端口上工作?

【问题讨论】:

    标签: spring spring-boot jersey metrics


    【解决方案1】:
    "com.sun.jersey.config.property.WebPageContentRegex"
    

    这是错误的属性。这是针对 Jersey 1.x 的。对于 2.x,它应该是

    "jersey.config.servlet.filter.staticContentRegex"
    

    ServletProperties.FILTER_STATIC_CONTENT_REGEX

    顺便说一句,您可以通过简单地设置几个配置属性来避免定义自己的FilterRegistrationBean。在您的application.properties 中,您可以使用以下内容

    spring.jersey.type=filter
    spring.jersey.init.jersey.config.servlet.filter.staticContentRegex=<your-regex>
    

    或者您可以在 ResourceConfig 子类中配置正则表达式

    public class JerseyConfig extends ResourceConfig {
        public JerseyConfig() {
            property(ServletProperties.FILTER_STATIC_CONTENT_REGEX, "<your-regex>");
        }
    }
    

    另一方面,仅供参考,问题的原因是用于泽西岛的默认/* url-mapping。如果您可以更改它,那么这样做可以解决问题。例如/api。您可以在属性中使用spring.jersey.applicationPath=/apiResourceConfig 子类上的@ApplicationPath("/api") 进行配置。

    除了final,还有一个属性

    ServletProperties.FILTER_FORWARD_ON_404
    "jersey.config.servlet.filter.forwardOn404"
    

    我不确定 staticContenRegex 属性是如何工作的,我从未真正深入研究过源代码。所以我不知道它是只是做一些文件 IO 来获取静态文件还是转发请求,但是如果它做一些文件 IO,那么我认为该属性不适用于您的用例,因为端点不是文件。在这种情况下 forwardOn404 应该可以工作。

    【讨论】:

    • 感谢所有建议。我通过在application.properties中添加简单的一行解决了这个问题: server.servlet-path=/admin 这行表示所有spring servlet都映射到/admin/...
    猜你喜欢
    • 2016-07-24
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    相关资源
    最近更新 更多