Skip to main content

Jersey PerRequestTypeInjectableProvider

No replies
Morf_2k7
Offline
Joined: 2011-06-15
Points: 0

Good day.

There is a class
@ Provider
public class OAuth {

private String name;

public OAuth () {
}

public String getName () {
return name;
}

public void setName (String name) {
</span>this.name = name;
}
}

Created for this class provider
@ Provider
public class MyPerReqProvider extends PerRequestTypeInjectableProvider <Context, OAuth> {

public MyPerReqProvider () {
super (OAuth.class);
}

@ Override
public Injectable <OAuth> getInjectable (ComponentContext componentContext, Context context) {
return new Injectable <OAuth> () {
@ Override
public OAuth getValue () {
return new OAuth ();
}
};
}
}

I declare the filter

@ Context OAuth auth;
public RequestFilter () {
}
@ Override
public ContainerRequest filter (ContainerRequest request) {
auth.setName ("admin");
}

and then in the method call
@ Context OAuth auth;

@ Path ("/ xml")
@ POST
@ Produces (MediaType.APPLICATION_XML)
public XmlResponse getMessageXML (@ Context HttpServletRequest request) {
System.out.println (auth.getName ());
}

and upon request receive an error
15.06.2011 14:22:51 com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and / or provider classes:
SEVERE: Missing dependency for field: ru.mw.http.OAuth ru.mw.http.RequestFilter.auth

here's another
javax.servlet.ServletException: Servlet.init () for servlet Jersey Web Application threw exception
org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java: 108)
org.apache.catalina.valves.AccessLogValve.invoke (AccessLogValve.java: 556)
org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java: 401)
org.apache.coyote.http11.Http11AprProcessor.process (Http11AprProcessor.java: 281)
org.apache.coyote.http11.Http11AprProtocol $ Http11ConnectionHandler.process (Http11AprProtocol.java: 579)
org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run (AprEndpoint.java: 1568)
java.util.concurrent.ThreadPoolExecutor $ Worker.runTask (ThreadPoolExecutor.java: 886)
java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java: 908)
java.lang.Thread.run (Thread.java: 662)

root cause

com.sun.jersey.spi.inject.Errors $ ErrorMessagesException
com.sun.jersey.spi.inject.Errors.processErrorMessages (Errors.java: 170)
com.sun.jersey.spi.inject.Errors.postProcess (Errors.java: 136)
com.sun.jersey.spi.inject.Errors.processWithErrors (Errors.java: 199)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate (WebApplicationImpl.java: 769)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate (WebApplicationImpl.java: 764)
com.sun.jersey.spi.container.servlet.ServletContainer.initiate (ServletContainer.java: 488)
com.sun.jersey.spi.container.servlet.ServletContainer $ InternalWebComponent.initiate (ServletContainer.java: 318)
com.sun.jersey.spi.container.servlet.WebComponent.load (WebComponent.java: 609)
com.sun.jersey.spi.container.servlet.WebComponent.init (WebComponent.java: 210)
com.sun.jersey.spi.container.servlet.ServletContainer.init (ServletContainer.java: 373)
com.sun.jersey.spi.container.servlet.ServletContainer.init (ServletContainer.java: 556)
javax.servlet.GenericServlet.init (GenericServlet.java: 151)
org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java: 108)
org.apache.catalina.valves.AccessLogValve.invoke (AccessLogValve.java: 556)
org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java: 401)
org.apache.coyote.http11.Http11AprProcessor.process (Http11AprProcessor.java: 281)
org.apache.coyote.http11.Http11AprProtocol $ Http11ConnectionHandler.process (Http11AprProtocol.java: 579)
org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run (AprEndpoint.java: 1568)
java.util.concurrent.ThreadPoolExecutor $ Worker.runTask (ThreadPoolExecutor.java: 886)
java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java: 908)
java.lang.Thread.run (Thread.java: 662)

If, instead of using PerRequestTypeInjectableProvider SingletonTypeInjectableProvider then all is well, please answer what I am doing wrong

All classes define both
public class initApp extends Application {

private Set <Object> singletons = new HashSet <Object> ();
private Set <Class <?>> classes = new HashSet <Class <?>>();

public initApp () {
classes.add (MyPerReqProvider.class);
classes.add (receiver.class);
classes.add (NotFoundExceptionMapper.class);
}

@ Override
public Set <Class <?>> getClasses () {
return classes;
}

@ Override
public Set <Object> getSingletons () {
return singletons;
}

Thanks in advance.