I have a resource in my Jersey REST api that has a private instance variable:
@Path("test")
public class TestResource implements ServletContextListener
{
private String someString;
@GET
public void test()
{
System.out.println("someString " + someString);
}
@Override
public void contextDestroyed(ServletContextEvent ctxt) {
System.out.println("Destroying context");
}
@Override
public void contextInitialized(ServletContextEvent ctxt) {
System.out.println("TestResource initialized!");
someString = "SET";
System.out.println("someString has been set. someString: " + someString);
}
}
On server startup/restart the instance variable someString
is initialized during contextInitialized()
and is correctly printed out. However when I set a GET
request to http://localhost:8080/myapp/api/test
(i.e. invoke the test()
method above) the variable someString
is null
.
How can I prevent that?