Stay easy and initialize settings - how to access?

I would like to have some init parameters in my web.xml and get them later in the application, I know that I can do this when I have a normal servlet. However, with resteasy, I configure HttpServletDispatcher as my default servlet, so I'm not quite sure how I can access this from my rest resource. It could be completely simple or I might need to use a different approach, as it would be nice to know what you guys think. Below 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 to install something in init-param and then restore it later in the backup resource. Any hints would be appreciated. Thanks guys!

+6
servlets jboss config resteasy
source share
1 answer

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

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

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

Or something 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(); } 
+21
source share

All Articles