I have some web-service (JAX-RS, WildFly 9, Resteasy)
@RequestScoped
public class SomeService{
// operations
}
Now I want to extract context information like user agent, which can be done using
@Context
private HttpHeaders httpHeaders;
It seems only be possible to inject this context in JAX-RS-related classes, but not in CDI beans called by the webservice. It is possible to put it into the webservice but this clutters the service with stuff not related to the core response of the service.
After some searching, I ended up using the javax.ws.rs.ext.Provider
annotation. It seems that the produced ContextInformation
object can then be used in other CDI-beans, not just in JAX-RS beans.
@Provider
public class ContextInformationProducer {
@Produces
@RequestScoped
public ContextInformation create() {
ContextInformation contextInformation = new ContextInformation();
contextInformation.setBrowserUserAgent(httpHeaders.getHeaderString("User-Agent"));
}
The question is if this is good practice? Or is it just a coincidence that this works? If it is not good practice, how can I do that in a better way? After looking at What does Provider in JAX-RS mean?, I am not sure if I am 'extending and customizing the JAX-RS runtime'. Should this used by application developers?