轻松和初始化参数 - 如何访问? [英] Rest easy and init params - how to access?

查看:16
本文介绍了轻松和初始化参数 - 如何访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 web.xml 中有一些 init 参数并稍后在应用程序中检索它们,我知道当我有一个普通的 servlet 时我可以做到这一点.但是,使用 resteasy 我将 HttpServletDispatcher 配置为我的默认 servlet,所以我不太确定如何从我的其余资源访问它.这可能非常简单,或者我可能需要使用不同的方法,无论哪种方式,了解你们的想法都会很好.以下是我的web.xml,

I'd like to have some init params in my web.xml and retrieve them later in the application, I know I can do this when I have a normal servlet. However with resteasy I configure HttpServletDispatcher to be my default servlet so I'm not quite sure how I can access this from my rest resource. This might be completely simple or I might need to use a different approach, either way it would be good to know what you guys think. Following is my web.xml,

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>RestEasy sample Web Application</display-name>
<!-- <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>true</param-value>
</context-param>  -->

 <listener>
     <listener-class>
         org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
     </listener-class>
 </listener>

 <servlet>
     <servlet-name>Resteasy</servlet-name>
     <servlet-class>
         org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
     </servlet-class>
     <init-param>
         <param-name>javax.ws.rs.Application</param-name>
         <param-value>com.pravin.sample.YoWorldApplication</param-value>
     </init-param>
 </servlet>

 <servlet-mapping>
     <servlet-name>Resteasy</servlet-name>
     <url-pattern>/*</url-pattern>
 </servlet-mapping>

</web-app>

我的问题是如何在 init-param 中设置一些东西,然后在一个安静的资源中检索它.任何提示将不胜感激.谢谢各位!

My question is how do I set something in the init-param and then retrieve it later in a restful resource. Any hints would be appreciated. Thanks guys!

推荐答案

使用@Context 批注将您想要的任何内容注入到您的方法中:

Use the @Context annotation to inject whatever you want into your method:

@GET
public Response getWhatever(@Context ServletContext servletContext) {
   String myParm = servletContext.getInitParameter("parmName");
}

使用@Context,您可以注入 HttpHeaders、UriInfo、Request、HttpServletRequest、HttpServletResponse、ServletConvig、ServletContext、SecurityContext.

With @Context you can inject HttpHeaders, UriInfo, Request, HttpServletRequest, HttpServletResponse, ServletConvig, ServletContext, SecurityContext.

或者其他任何东西,如果你使用这个代码:

Or anything else if you use this code:

public class MyApplication extends Application {
  public MyApplication(@Context Dispatcher dispatcher) {
    MyClass myInstance = new MyClass();
    dispatcher.getDefautlContextObjects().
         put(MyClass.class, myInstance);
  }
}

@GET
public Response getWhatever(@Context MyClass myInstance) {
   myInstance.doWhatever();
}

这篇关于轻松和初始化参数 - 如何访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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