One thing I’ve forgotten to document thoroughly is how to add objects that are injectable via the @javax.ws.rs.core.Context. Usually you’ll want to use CDI or Spring to inject your dependencies or configuration into a provider or a service, but you may have situations where you cannot depend on these facilities being available to you.
import org.jboss.resteasy.core.Dispatcher; import javax.ws.rs.core.Application; import javax.ws.rs.core.Context; import java.io.InputStream; import java.util.HashSet; import java.util.Set; public class MyApplication extends Application { public MyApplication(@Context Dispatcher dispatcher) { MyClass myInstance = new MyClass(); dispatcher.getDefaultContextObjects().put(MyClass.class, myInstance); } }
The myInstance variable is now available for injection via the @Context annotation.
Nov 15, 2012 @ 11:52:13
I am trying to migrate an application from Jersey to RestEasy
Jersey has SingletonTypeInjectableProvider interface for singleton objects
Nearly two days i am trying to find similar thing but i couldnt
Your solution save my life 🙂
Thanks
Nov 15, 2012 @ 20:49:02
You could also use Spring, CDI, Guice, whatever… 🙂