Unable to find contextual data of type: java.ws.rs

2019-07-16 02:40发布

问题:

I am using wildfly 9.0.1.Final

I am deploying a REST application which has an endpoint defined like this:

@GET
@Path("/view/products")
@Produces(MediaType.APPLICATION_JSON)
@CORSEnabled
@Protected
public String getConfiguredProducts(
    @Context ContainerRequestContext request) {
    if (request != null) {
        Integer companyId = (Integer) request.getProperty("company");
        if (companyId != null) {
             //do stuff
        }
    }
    // ... more staff
}

When the application runs, the context is injected, i.e. 'request' is non null.

When I try though to retrieve the property I get back the error:

executing GET /complaints/view/products:        
org.jboss.resteasy.spi.LoggableFailure: Unable to find contextual data    
of type: javax.ws.rs.container.ContainerRequestContext

Please note that the application was running without problems on glassfish/jersey and the problem appeared when I tried to move it to wildfly/resteasy.

Any ideas?

回答1:

I am answering my own question, since it seems that RestEasy does not support injection of a ContainerRequestContext, at least according to this

https://docs.jboss.org/resteasy/docs/1.1.GA/userguide/html_single/#_Context

The @Context annotation allows you to inject instances of javax.ws.rs.core.HttpHeaders, javax.ws.rs.core.UriInfo, javax.ws.rs.core.Request, javax.servlet.HttpServletRequest, javax.servlet.HttpServletResponse, javax.servlet.ServletConfig, javax.servlet.ServletContext, and javax.ws.rs.core.SecurityContext objects.

I injected a javax.ws.rs.core.HttpHeaders instead, where I had previously added all the information I needed.



回答2:

I also had the error RESTEASY003880: Unable to find contextual data of type: javax.ws.rs.core.HttpHeaders

It was caused by accessing the headers in a different thread.

Hopefully the answer will help when googling the problem.