5

My web container knows whether my application is running in debug or release mode. I'd like to pass this information to my ResourceConfig/Application class, but it's not clear how to read this information back out.

Is it possible to pass information by way of servlet/filter parameters? If so, how?

3 Answers 3

3

Here's how I'm doing it:

in 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> 

and in my Application subclass:

@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 
Sign up to request clarification or add additional context in comments.

3 Comments

I unaccepted this answer, as I'm getting a ServletContext value of null. Looking at stackoverflow.com/q/19450202/14731 I'm not the only one.
You may want to be more specific in your question then. My code is working for me, is Java EE spec compliant and does what you asked in your question. I noticed you gave more details in a post to the Jersey mailing list that you don't mention here: you want the property to become available in the constructor?Why not accept my answer for the question you posted here and ask a new question with all the details upfront?
If you definitely want the parameters in the Application constructor, I think you'll need to look at one of the various other suggestions (passing them as system properties to the JVM, use of a property file, etc.). But if you just need those parameters at application start-up time (as opposed to per request for instance), a @Provider class (where injection is supported in constructors) may help. In GlassFish 3.1.2.2 with Jersey 1.11.1 where I tried, these are instantiated straight after the instantiation of your Application subclass (where I return my @Provider class from getSingletons).
3

This was fixed in Jersey 2.5: https://java.net/jira/browse/JERSEY-2184

You should now be able to inject @Context ServletContext into the Application constructor.

Here is an example of how this is expected to work:

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

You can also invoke ServiceLocator.getService(ServletContext.class) to get the ServletContext from any point in the application.

1 Comment

Good to know! Could you provide an example in order to actually answer the question posed?
0

In Jersey 1 it was possible to pass @Context ServletContext servletContext to the constructor of an Application class, but in Jersey 2 this no longer works. It seems Jersey 2 will only inject at request time.

To work around this in Jersey 2, use an anonymous ContainerRequestFilter to gain access to a ServletContext at request time and pass the required init parameters into the Application class.

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; }); }; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.