【问题标题】:Jersey 2: How to pass parameters from web.xml to Application?Jersey 2:如何将参数从 web.xml 传递到应用程序?
【发布时间】:2013-10-25 18:01:44
【问题描述】:

我的 Web 容器知道我的应用程序是在调试模式还是在发布模式下运行。我想将此信息传递给我的 ResourceConfig/Application 类,但不清楚如何将这些信息读回。

是否可以通过 servlet/filter 参数传递信息?如果有,怎么做?

【问题讨论】:

    标签: jersey jax-rs jersey-2.0


    【解决方案1】:

    这是我的做法:

    web.xml:

    <context-param>
      <description>When set to true, all operations include debugging info</description>
      <param-name>com.example.DEBUG_API_ENABLED</param-name>
      <param-value>true</param-value>
    </context-param>
    

    在我的Application 子类中:

    @ApplicationPath("api")
    public class RestApplication extends Application {
      @Context
      protected ServletContext sc;
    
      @Override
      public Set<Class<?>> getClasses() {
        Set<Class<?>> set = new HashSet<Class<?>>();
        boolean debugging = Boolean.parseBoolean(sc
                .getInitParameter("com.example.DEBUG_API_ENABLED"));
    
        if (debugging) {
            // enable debugging resources
    

    【讨论】:

    • 我不接受这个答案,因为我得到的 ServletContext 值为 null。看着stackoverflow.com/q/19450202/14731我不是唯一的。
    • 您可能希望在您的问题中更具体。我的代码对我有用,符合 Java EE 规范,并且符合您在问题中提出的要求。我注意到您在泽西邮件列表的帖子中提供了更多详细信息,但您未在此处提及:您希望该属性在构造函数中可用?为什么不接受我对您在此处发布的问题的回答并提出新问题所有的细节都提前?
    • 如果您确实需要 Application 构造函数中的参数,我认为您需要查看其他各种建议之一(将它们作为系统属性传递给 JVM,使用属性文件, ETC。)。但是,如果您只在应用程序启动时需要这些参数(而不是例如每个请求),那么 @Provider 类(在构造函数中支持注入)可能会有所帮助。在我尝试过的带有 Jersey 1.11.1 的 GlassFish 3.1.2.2 中,这些是在您的 Application 子类实例化之后直接实例化的(我从 getSingletons 返回我的 @Provider 类)。
    【解决方案2】:

    这在 Jersey 2.5 中已修复:https://java.net/jira/browse/JERSEY-2184

    您现在应该能够将@Context ServletContext 注入Application 构造函数。

    以下是预期如何工作的示例:

    public class MyApplication extends Application
    {
      private final String myInitParameter;
    
      public MyApplication(@Context ServletContext servletContext)
      {
        this.myInitParameter = servletContext.getInitParameter("myInitParameter");
      }
    }
    

    您还可以调用ServiceLocator.getService(ServletContext.class) 以从应用程序中的任何位置获取ServletContext

    【讨论】:

    • 很高兴知道!您能否提供一个示例以实际回答所提出的问题?
    【解决方案3】:

    在 Jersey 1 中,可以将 @Context ServletContext servletContext 传递给 Application 类的构造函数,但在 Jersey 2 中,这不再有效。看来 Jersey 2 只会在请求时注入。

    要在 Jersey 2 中解决此问题,请使用匿名 ContainerRequestFilter 在请求时访问 ServletContext 并将所需的 init 参数传递给 Application 类。

    public class MyApplication extends Application {
      @Context protected ServletContext servletContext;
      private String myInitParameter;
    
      @Override
      public Set<Object> getSingletons() {
        Set<Object> singletons = new HashSet<Object>();
        singletons.add(new ContainerRequestFilter() {
          @Override
          public void filter(ContainerRequestContext containerRequestContext) throws IOException {
            synchronized(MyApplication.this) {
              if(myInitParameter == null) {
                myInitParameter = servletContext.getInitParameter("myInitParameter");
                // do any initialisation based on init params here
              }
            }
          }
          return singletons;
        });
      };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多