Jersey 2:如何将参数从 web.xml 传递到应用程序?

Gil*_*ili 5 jax-rs jersey jersey-2.0

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

是否可以通过 servlet/filter 参数传递信息?如果是这样,如何?

Gil*_*ili 3

这已在 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");
  }
}
Run Code Online (Sandbox Code Playgroud)

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