Hi Remon,
The problem here is one of word overloading. The Context in
ContextResolver and the Context in @Context are not the same thing.
JAX-RS does not provide a general DI mechanism and you need to use
something like Guice, CDI or Spring for such purposes.
If you want to use ContextResolver you use it like this:
@Context ContextResolver<TestInjectedObject> cr;
JAX-RS aggregates multiple ContextResolver<T> where T is the same
type. The rational for this was to enable the application to declare
one or more JAXBContext for sets of Java types. So you register
implementations of ContextResolver<JAXBContext> and the JAXB message
body readers/writers will use any such context resolvers to obtain a
JAXBContext (or Marshaller Unmarshaller) to read/write XML.
Hth,
Paul.
On Oct 20, 2010, at 11:56 AM, Remon van Vliet wrote:
> Hi all,
>
> I'm having trouble getting ContextResolver implementations to work.
> I want to be able to inject arbitrary classes into my JAX-RS
> resources using @Context but regardless of how I try to do this it
> always causes either exceptions or the field remains null. I'm using
> the servlet container. I've reduced it to the simplest possible case :
>
> @Provider
> @Produces(MediaType.APPLICATION_JSON)
> public class TestResolver implements
> ContextResolver<TestInjectedObject> {
>
> @Override
> public TestInjectedObject getContext(Class<?> c) {
>
> return new TestInjectedObject();
> }
> }
>
> and in a resource :
>
> @Context
> TestInjectedObject test;
>
> Depending on the jersey version and some settings fiddling this
> either remains null or throws this exception :
>
> WARNING: StandardWrapperValve[config]: PWC1382: Allocate exception
> for servlet config
> com.sun.jersey.spi.inject.Errors$ErrorMessagesException
> at
> com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
> at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:137)
> at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:
> 203)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application.WebApplicationImpl.initiate(WebApplicationImpl.java:695)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application.WebApplicationImpl.initiate(WebApplicationImpl.java:690)
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.initiate(ServletContainer.java:
> 438)
> at com.sun.jersey.spi.container.servlet.ServletContainer
> $InternalWebComponent.initiate(ServletContainer.java:287)
> at
> com
> .sun
> .jersey.spi.container.servlet.WebComponent.load(WebComponent.java:587)
> at
> com
> .sun
> .jersey.spi.container.servlet.WebComponent.init(WebComponent.java:213)
> at
> com
> .sun
> .jersey
> .spi.container.servlet.ServletContainer.init(ServletContainer.java:
> 342)
> at
> com
> .sun
> .jersey
> .spi.container.servlet.ServletContainer.init(ServletContainer.java:
> 516)
> at javax.servlet.GenericServlet.init(GenericServlet.java:242)
> at
> com
> .exmachina
> .play2tv.commons.services.ServiceServlet.init(ServiceServlet.java:19)
> at
> org
> .apache
> .catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1428)
> at
> org
> .apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:
> 1060)
> at
> org
> .apache
> .catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
> 187)
> at
> org
> .apache
> .catalina.core.StandardContextValve.invoke(StandardContextValve.java:
> 188)
> at
> org
> .apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:
> 641)
> at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
> at
> com
> .sun
> .enterprise
> .web
> .PESessionLockingStandardPipeline
> .invoke(PESessionLockingStandardPipeline.java:85)
> at
> org
> .apache
> .catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
> at
> org
> .apache
> .catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
> at
> org
> .apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
> 226)
> at
> com
> .sun
> .enterprise
> .v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
> at
> com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:
> 791)
> at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:
> 693)
> at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
> at
> com
> .sun
> .grizzly
> .http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
> at
> com
> .sun
> .grizzly
> .DefaultProtocolChain
> .executeProtocolFilter(DefaultProtocolChain.java:135)
> at
> com
> .sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:
> 102)
> at
> com
> .sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:
> 88)
> at
> com
> .sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
> at
> com
> .sun
> .grizzly
> .ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
> at
> com
> .sun
> .grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
> at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
> at com.sun.grizzly.util.AbstractThreadPool
> $Worker.doWork(AbstractThreadPool.java:330)
> at com.sun.grizzly.util.AbstractThreadPool
> $Worker.run(AbstractThreadPool.java:309)
> at java.lang.Thread.run(Thread.java:619)
>
> Am I trying to do something that isn't supposed to work in the first
> place or am I doing something wrong? Also, if it is the first, why
> shouldn't I be able to use the ContextResolver interface to do
> custom injection?
>
> Remon.