How can/should I pass an object from a ContainerRequestFilter to a (post-matching) resource in (JAX-RS) Resteasy version 3.0.11 that has undertow embedded and uses Guice?
1 Answer
The method ContainerRequestContext#setProperty stores values which are synced with the HttpServletRequest
. So with plain JAX-RS you can store an attribute like this:
@Provider
public class SomeFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
requestContext.setProperty("someProperty", "someValue");
}
}
And afterwards you can obtain it in your resource class:
@GET
public Response someMethod(@Context org.jboss.resteasy.spi.HttpRequest request) {
return Response.ok(request.getAttribute("someProperty")).build();
}
With CDI you also can inject any bean in the filter and resource class:
@Provider
public class SomeFilter implements ContainerRequestFilter {
@Inject
private SomeBean someBean;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
someBean.setFoo("bar");
}
}
In your resource class:
@Inject
private SomeBean someBean;
@GET
public Response someMethod() {
return Response.ok(someBean.getFoo()).build();
}
I'd expect the same to be working with Guice.
Update: As @bakil pointed out correctly you should use a @RequestScoped
bean if the object you want to pass should only be associated with the current request.
-
In the CDI example, I guess the requestContext/request parameters are just distractions, and I guess SomeBean needs to be annotated
@RequestScoped
or similar? Aug 7, 2015 at 13:54 -
Thanks for the hint. I removed the senseless parameters. SomeBean needs to be a CDI bean which means it can have any CDI scope.
@RequestScoped
might make sense in most of the cases.– leflohAug 7, 2015 at 14:31 -
If the default
@Dependent
scope is used, then the filter and the resource class will receive distinct SomeBean instances, so the data won't be transferred. I think you need@RequestScoped
to ensure that the same instance is shared by both. Aug 7, 2015 at 16:16 -
Oh sure,
@Dependent
would only work if the filter and the resource class would have the same scope which is not the case. But I tried and it's working with a@ApplicationScoped
,@Singleton
and@SessionScoped
SomeBean (if this ever makes sense).– leflohAug 7, 2015 at 16:34 -
No, those only appear to work, but they are actually broken. If you have multiple threads simultaneously making a request, then you risk the setFoo/getFoo getting mixed up. Aug 7, 2015 at 16:39