Friday, February 8, 2019

Jersey 2.26 and injection changes

What changed in Jersey 2.26+ ?

    If you look at the release notes for Jersey 2.26 there is a brief note about changing the dependency injection mechanism. There is not much detail on how this is going to impact a user of some of Jersey's features - specifically injecting method parameters. Quoting from the release notes:
attempt to make Jersey core independent of any specific injection framework. As you might now, Jersey 2.x is (was!) pretty tightly dependent on HK2, which sometimes causes issues (esp. when running on other injection containers. Jersey now defines it's own injection facade, which, when implemented properly, replaces all internal Jersey injection.

Jersey source commits of interest

    For the adventurous souls, the following is a list of commits that changed the classes, interfaces and mechanisms around field and method parameters injection.

It is fascinating to trace the changes that were gradually introduced, including the name changes, simplification and rationalization.

The core changes are around AbstractValueFactoryProvider, ValueFactoryProvider and the mechanism for actually getting to the value to be injected.

So what should I do?

  1. Replace usage of org.glassfish.jersey.server.spi.internal.ValueFactoryProvider with org.glassfish.jersey.server.spi.internal.ValueParamProvider.
  2. Replace usage of org.glassfish.jersey.server.internal.inject.AbstractValueFactoryProvider with org.glassfish.jersey.server.internal.inject.AbstractValueParamProvider.
  3. If your implementation of AbstractValueFactoryProvider had a super call, note that the constructor signature has changed. The constructor parameters are now Provider mpep, Parameter.Source... compatibleSources.
The best is yet to come.
.
The return type of AbstractValueParamProvider.createValueProvider() is now
java.util.function.Function
So you need to create a FunctionalInterface that returns the object that you need.

Once these changes are made your parameter injection will work just as before. To make things more future-proof it will be best to use the new AbstractBinder and Binder that Jersey 2.26 has introduced.  This will decouple your code very firmly from HK2.

It is still a matter of concern that these classes are 'internal' Jersey classes. Thus there is lack of documentation and no clear warning on changes.

No comments:

Post a Comment