In Jersey 1, you could pass it to @Context ServletContext servletContextthe class constructor Application, but in Jersey 2 it no longer works. It seems that Jersey 2 will only enter at the time of the request.
To get around this on Jersey-2, use anonymous ContainerRequestFilterto access the ServletContext during the request and pass the necessary init parameters to the class Application.
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");
}
}
}
return singletons;
});
};
}
source
share