问题
I am using jersey-guice to set up all my Jersey 1 resources using a bunch of Guice (Servlet)Modules.
I have written a custom ViewProcessor (CustomViewProcessor) that relies on a configuration object, that I want to be injected into it using Guice. This CustomViewProcessor should be picked up and used by Jersey every time it needs one.
How do I tell Jersey to fetch a Guice-created CustomViewProcessor instance whenever it needs a ViewProcessor? I want to set all this up within my Guice Modules' configure methods.
回答1:
I found out that this is rather easy: Just create that CustomViewProcessor class and annotate it with @Provider. By binding it with Guice (and having jersey-guice installed), your CustomViewProcessor will be instantiated and used in the right places.
The CustomViewProcessor class can use all the @Injected fields that you wish, like configuration objects.
来源:https://stackoverflow.com/questions/22534429/install-custom-jersey-viewprocessor-from-guice-servletmodule