@Context/@Inject not working on interfaces

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

@Context/@Inject not working on interfaces

Trenton D. Adams
I noticed that @Context and @Inject do not work on interfaces.  Is that intentional?  It works perfectly if I put the annotations on the member variables themselves.

I define this on my interface, and it does not get called on the implementing class.

On the interface I have...
void setServiceUri(@Context UriInfo serviceUri);

void setRequest(@Context HttpServletRequest request);

@Inject
void setSession(HttpSession session);

On the implementing class I have...
@Override
public void setServiceUri(UriInfo serviceUri)
{
this.serviceUri = serviceUri;
}

@Override
public void setRequest(HttpServletRequest request)
{
this.request = request;
}

@Override
public void setSession(HttpSession session)
{
this.session = session;
}

I set a break point on any of the set methods, and none of them gets called.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @Context/@Inject not working on interfaces

Robert Gacki
Hi,

the @Inject / @Context annotations are an implementation detail and
should not be used to annotate interfaces. So don't expect Jersey to
traverse into the class hierarchy to collect those annotations.

Robert

Am Freitag, den 10.06.2016, 01:35 -0600 schrieb Trenton D. Adams:

> I noticed that @Context and @Inject do not work on interfaces.  Is
> that intentional?  It works perfectly if I put the annotations on the
> member variables themselves.
>
> I define this on my interface, and it does not get called on the
> implementing class.
>
> On the interface I have...
> void setServiceUri(@Context UriInfo serviceUri);
>
> void setRequest(@Context HttpServletRequest request);
>
> @Inject
> void setSession(HttpSession session);
>
> On the implementing class I have...
> @Override
> public void setServiceUri(UriInfo serviceUri)
> {
>     this.serviceUri = serviceUri;
> }
>
> @Override
> public void setRequest(HttpServletRequest request)
> {
>     this.request = request;
> }
>
> @Override
> public void setSession(HttpSession session)
> {
>     this.session = session;
> }
>
> I set a break point on any of the set methods, and none of them gets
> called.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @Context/@Inject not working on interfaces

Trenton D. Adams
Okay, thanks.

On Fri, Jun 10, 2016 at 4:12 AM, Robert Gacki <[hidden email]> wrote:
Hi,

the @Inject / @Context annotations are an implementation detail and
should not be used to annotate interfaces. So don't expect Jersey to
traverse into the class hierarchy to collect those annotations.

Robert

Am Freitag, den 10.06.2016, 01:35 -0600 schrieb Trenton D. Adams:
> I noticed that @Context and @Inject do not work on interfaces.  Is
> that intentional?  It works perfectly if I put the annotations on the
> member variables themselves.
>
> I define this on my interface, and it does not get called on the
> implementing class.
>
> On the interface I have...
> void setServiceUri(@Context UriInfo serviceUri);
>
> void setRequest(@Context HttpServletRequest request);
>
> @Inject
> void setSession(HttpSession session);
>
> On the implementing class I have...
> @Override
> public void setServiceUri(UriInfo serviceUri)
> {
>     this.serviceUri = serviceUri;
> }
>
> @Override
> public void setRequest(HttpServletRequest request)
> {
>     this.request = request;
> }
>
> @Override
> public void setSession(HttpSession session)
> {
>     this.session = session;
> }
>
> I set a break point on any of the set methods, and none of them gets
> called.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @Context/@Inject not working on interfaces

Robert Gacki
Just an advise: You can use constructor injection with @Inject. Not
also makes instantiation dependencies explicit, it also enables you to
have final references to those dependencies.

@Path("/profile")
public class ProfileResource {

  private final UserService userService;

  @Inject
  public DefaultFoo(final UserService userService) {
    checkArgument(userService != null);
    this.userService = userService;
  }

  @GET
  public Response doSomething(final @Context HttpSession session) {
    ....
  }

}

BTW. In my (Dropwizard) applications, it is pretty rare that resource
classes implement interfaces. There are not many usecases, because
resource classes tend to become really hard to abstract. And I count
that as a good thing, because it reduces complexity to a minimum.

Robert


Am Freitag, den 10.06.2016, 12:12 -0600 schrieb Trenton D. Adams:

> Okay, thanks.
>
> On Fri, Jun 10, 2016 at 4:12 AM, Robert Gacki <robert.gacki@contenttr
> ace.org> wrote:
> > Hi,
> >
> > the @Inject / @Context annotations are an implementation detail and
> > should not be used to annotate interfaces. So don't expect Jersey
> > to
> > traverse into the class hierarchy to collect those annotations.
> >
> > Robert
> >
> > Am Freitag, den 10.06.2016, 01:35 -0600 schrieb Trenton D. Adams:
> > > I noticed that @Context and @Inject do not work on interfaces. 
> > Is
> > > that intentional?  It works perfectly if I put the annotations on
> > the
> > > member variables themselves.
> > >
> > > I define this on my interface, and it does not get called on the
> > > implementing class.
> > >
> > > On the interface I have...
> > > void setServiceUri(@Context UriInfo serviceUri);
> > >
> > > void setRequest(@Context HttpServletRequest request);
> > >
> > > @Inject
> > > void setSession(HttpSession session);
> > >
> > > On the implementing class I have...
> > > @Override
> > > public void setServiceUri(UriInfo serviceUri)
> > > {
> > >     this.serviceUri = serviceUri;
> > > }
> > >
> > > @Override
> > > public void setRequest(HttpServletRequest request)
> > > {
> > >     this.request = request;
> > > }
> > >
> > > @Override
> > > public void setSession(HttpSession session)
> > > {
> > >     this.session = session;
> > > }
> > >
> > > I set a break point on any of the set methods, and none of them
> > gets
> > > called.
> >
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @Context/@Inject not working on interfaces

Trenton D. Adams
Some good points.  I agree that most classes would not need an interface.  Simply sub-classes has solved any issues I've ran into so far.  For example, I'm providing a POST through a resource class, and the sub-class does something different.  One redirects to another page, so the user can see the results (I'm using Jersey templates).  The other is meant to be called by a utility program, not a browser, so it simply returns the result.

On Fri, Jun 10, 2016 at 1:17 PM, Robert Gacki <[hidden email]> wrote:
Just an advise: You can use constructor injection with @Inject. Not
also makes instantiation dependencies explicit, it also enables you to
have final references to those dependencies.

@Path("/profile")
public class ProfileResource {

  private final UserService userService;

  @Inject
  public DefaultFoo(final UserService userService) {
    checkArgument(userService != null);
    this.userService = userService;
  }

  @GET
  public Response doSomething(final @Context HttpSession session) {
    ....
  }

}

BTW. In my (Dropwizard) applications, it is pretty rare that resource
classes implement interfaces. There are not many usecases, because
resource classes tend to become really hard to abstract. And I count
that as a good thing, because it reduces complexity to a minimum.

Robert


Am Freitag, den 10.06.2016, 12:12 -0600 schrieb Trenton D. Adams:
> Okay, thanks.
>
> On Fri, Jun 10, 2016 at 4:12 AM, Robert Gacki <robert.gacki@contenttr
> ace.org> wrote:
> > Hi,
> >
> > the @Inject / @Context annotations are an implementation detail and
> > should not be used to annotate interfaces. So don't expect Jersey
> > to
> > traverse into the class hierarchy to collect those annotations.
> >
> > Robert
> >
> > Am Freitag, den 10.06.2016, 01:35 -0600 schrieb Trenton D. Adams:
> > > I noticed that @Context and @Inject do not work on interfaces. 
> > Is
> > > that intentional?  It works perfectly if I put the annotations on
> > the
> > > member variables themselves.
> > >
> > > I define this on my interface, and it does not get called on the
> > > implementing class.
> > >
> > > On the interface I have...
> > > void setServiceUri(@Context UriInfo serviceUri);
> > >
> > > void setRequest(@Context HttpServletRequest request);
> > >
> > > @Inject
> > > void setSession(HttpSession session);
> > >
> > > On the implementing class I have...
> > > @Override
> > > public void setServiceUri(UriInfo serviceUri)
> > > {
> > >     this.serviceUri = serviceUri;
> > > }
> > >
> > > @Override
> > > public void setRequest(HttpServletRequest request)
> > > {
> > >     this.request = request;
> > > }
> > >
> > > @Override
> > > public void setSession(HttpSession session)
> > > {
> > >     this.session = session;
> > > }
> > >
> > > I set a break point on any of the set methods, and none of them
> > gets
> > > called.
> >

Loading...