Jersey context injection in resources not working with Dropwizard 2.0

537 views
Skip to first unread message

Paolo Simonazzi

unread,
Aug 22, 2019, 10:04:31 AM8/22/19
to dropwizard-user
I'm testing dropwizard 2.0 and found out that the current version of Jersey does not support @Context injection when registering resources as object. 
It worked in the version included in Dropwizard 1.3.9.


Example resource:
@Path("/")
public class MyResource {
  @Context
  private HttpServletRequest httpServletRequest;
  ...
}

Registering as object
env.jersey().register(new MyResource());
the httpServletRequest will be null.

A workaround is to register the resource by class:
env.jersey().register(MyResource.class);
But it can be very cumbersome if the resource must be passed some parameters in the constructor (we need to inject them too, or pass them is some other way).

So I would like to know if someone found a more convenient way to manage this problem until it is (hopefully) fixed upstream.

Thanks

Jochen Schalanda

unread,
Aug 25, 2019, 1:12:20 PM8/25/19
to dropwiz...@googlegroups.com

James Kern

unread,
Sep 3, 2020, 12:09:21 PM9/3/20
to dropwizard-user
I'm wondering if there are examples of how to work around this when the resource class constructor takes parameters? Does this changes mean I need to integrate a DI framework?

Matthias Mueller

unread,
Sep 4, 2020, 4:30:58 AM9/4/20
to dropwiz...@googlegroups.com

AFAIK you *can* still register instances. (The documentation text is a bit misleading).

 

The way to work around your issue is documented in Jochen’s link: Instead of defining @context fields at the class level you have to move them to the method parameters.

 

- Matthias

--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dropwizard-user/ce07832e-97e9-4a1c-a51b-71e4d789084en%40googlegroups.com.

James Kern

unread,
Sep 4, 2020, 11:27:56 AM9/4/20
to dropwiz...@googlegroups.com
We have hundreds of endpoints and the code has kind of taken for granted the presence of these fields (i.e. resources all extend a base class with some common helper methods and the context gets added there). So it'll be a bit annoying to go through them all to explicitly add the context fields as parameters where needed. But you're probably right that that's a better option than having to move it all to hk2 injection. Thanks for reminding me of that option

- James

You received this message because you are subscribed to a topic in the Google Groups "dropwizard-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dropwizard-user/ASXuC6ZL97c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dropwizard-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dropwizard-user/79d2d5ca401b4369a011eb4f7b75f0ee%40MSX-L104.msx.ad.zih.tu-dresden.de.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages