1

so I have a RequestScoped bean that I am trying to inject UriInfo into using the Context annotation. The application is a JAX-RS based RESTful Web Service running on Wildfly 8 (and subsequently rest-easy). If I inject the UriInfo into the resource itself, it will inject correctly. If I try to inject it into any of the injected children, it will not inject and ends up null.

This was working for me on Web Sphere 8.5...but now it doesn't work on Wildfly 8. Any ideas? The source code is at https://github.com/rpg-maker-repo/rmmv-api. The resource is "com.trinary.rpgmaker.resource.PluginResource" and the place where I'm injecting the UriInfo is "com.trinary.rpgmaker.service.LinkGenerator". Currently I have the injection of UriInfo removed and the code that implemented it commented out. I have tried many ways of trying to inject it and none of them work.

3 Answers3

4

Yes, the jaxrs-cdi integration is not fully specified and the implementations are free to enhance it to the extent that they want. Jersey (e.g. glassfish) has a very powerful integration and you can inject jaxrs' @Context into cdi beans (see http://hnusfialovej.cz/2015/02/25/jersey-further-improves-cdi-integration/). Resteasy's (e.g. Wildfly) integration isn't so straightforward but you can achieve injection of UriInfo into cdi beans via jaxrs providers (see http://blog.christianbauer.name/Accessing%20request%20details%20with%20JAX-RS%20and%20CDI/) (tested on wfly 10).

rychu
  • 896
  • 1
  • 7
  • 16
  • That did the trick! Thank you so much for replying! I thought that I was just stupid for a while =). – deusprogrammer Jan 28 '17 at 19:10
  • Link is dead. Example code: `@Provider @ApplicationScoped public class URIInfoProvider implements ContainerRequestFilter { @Context @Produces @RequestScoped private UriInfo uriInfo; @Override public void filter(ContainerRequestContext requestContext) throws IOException { } } ` – cghislai Feb 20 '19 at 14:25
  • Christian Bauer's blog available through Wayback Machine: http://web.archive.org/web/20170907174715/http://blog.christianbauer.name/Accessing%20request%20details%20with%20JAX-RS%20and%20CDI/ – rychu Aug 29 '19 at 04:52
  • Or you can add `@Provider` to any bean in which you want to have `UriInfo` "injected" with `@Context`. On one hand this allows you to not create the producer but on the other you have to remember to use `@Context` and not `@Inject` everywhere for `UriInfo` (you'll get deploy time error if you forget). – rychu Sep 20 '19 at 05:04
1

So you have a JAX-RS resource A which injects a CDI bean B and you want to use @Context to inject UriInfo into B?

Is there any reference in the Java EE spec to indicate that this is supposed to work?

I don't think so.

@Context is specific to JAX-RS and is not mentioned in the CDI 1.2 spec. It is not supported by CDI (unlike @PersistenceContext, @Resource and other legacy injections).

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
0

You could try to use a Provider which accesses the @Context:

@javax.ws.rs.ext.Provider
public class ContextInformationProducer {
    @Produces
    @RequestScoped
    public ContextInformation create() {
        ContextInformation contextInformation = new ContextInformation();
        contextInformation.setBrowserUserAgent(httpHeaders.getHeaderString("User-Agent"));
}

Altough I am not sure if this is good practise, for which I have the following question: Using @Context in JAX-RS Provider to provide context information to CDI beans

Community
  • 1
  • 1
user140547
  • 7,750
  • 3
  • 28
  • 80