6

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>

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!

opensourcegeek
  • 5,552
  • 7
  • 43
  • 64
  • Just to make the question clearer, if I have multiple restful resources I would like to be able to set init-params for each of them and later retrieve inside that resource. If there is a better approach I'd be equally happy... so anybody?? – opensourcegeek Mar 29 '11 at 07:14

1 Answers1

22

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

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

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();
}
Mark Lutton
  • 6,959
  • 7
  • 41
  • 57
  • I see what you mean, would you be great if you could get this done through web.xml rather than code - but thanks for your response! – opensourcegeek Apr 05 '11 at 08:26
  • thanks forgot to do so! - I did move away from resteasy to apache cxf for various reasons, nevertheless it is the right answer. – opensourcegeek Dec 03 '12 at 11:18
  • @Mark, thank's for this answer but is there a way to set initParameter to ServletContext ? We can set attributes but there is no setInitParameter method provided. – jmcollin92 Jan 25 '13 at 13:22
  • If I try this method with a custom class, then I get the following error: `@Context annotation is only allowed on method parameters of type '[javax.ws.rs.core.HttpHeaders, javax.ws.rs.core.UriInfo, javax.ws.rs.core.Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.ServletConfig, javax.servlet.ServletContext, javax.ws.rs.core.SecurityContext]'.` So it doesn't really to seem to fit to custom created classes, is there a workaround? – amaik Aug 30 '17 at 07:24