0

I've just started using Jersey to implement a JAX-RS resource. One thing I haven't been able to figure out is how I give it access to some dependency in my Java application.

This is not a duplicate of How to inject an object into jersey request context? or Dependency injection with Jersey 2.0, at least as far as I can tell.

E.g. I would like to give the following resource access to an instance of MyObject, which is created somewhere else in the program.

@Path("myResource")
public class MyResource
{
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Response foo() 
    {
        // I want to call get() on an instance of MyObject
        // how do I get this instance of MyObject into the resource?
        myObject.get();
        return Response.status(Response.Status.OK).entity("response").build();
    }
}

This seems like such an obvious question but I can't find an answer.

Community
  • 1
  • 1
ksl
  • 4,519
  • 11
  • 65
  • 106
  • Are you using dependency injection? If so, which library? – Uri Shalit Feb 03 '16 at 18:08
  • 1
    How is this different from what's described in the DI With Jersey 2.0 answer? It seems much the same to me. – sisyphus Feb 03 '16 at 18:12
  • _"which is created somewhere else in the program"_ .. where? – Paul Samsotha Feb 03 '16 at 18:38
  • @UriShalit I'm not using a dependency injection library. I'm new to Java too - I didn't know there was such a thing. Why do you ask? – ksl Feb 03 '16 at 18:56
  • @sisyphus I don't have a `web.xml` file. – ksl Feb 03 '16 at 19:00
  • @peeskillet Why does that matter? As it happens, in this case it is created when the applications starts up but it doesn't have to be. My question still applies - how do I get a pre-existing instance of some class into my resource? – ksl Feb 03 '16 at 19:02
  • It matter because trying to bind something at application startup requires a different technique from trying to bind something at run time. The first link you provided is an example of the latter, and the first link is an example of the former. I am closing this as a duplicate, as the answers in the link have all the information you need. If you have problems implementing it, then please show what you have tried so we can help you. Otherwise in this state, an answer will be no more than a duplicate answer that is already in the link. Only thing I can say the answers don't show is that you need – Paul Samsotha Feb 03 '16 at 19:18
  • to use the `@Inject` annotation, to inject the object into your resource – Paul Samsotha Feb 03 '16 at 19:19
  • @peeskillet Your answer does not help me to find out what I need to know. Can you point me to something (without all the Jersey / JAX-RS noise) that explains why it matters where the object is created? It's not always easy to ask the right question when you don't know what you don't know. – ksl Feb 03 '16 at 20:33
  • You're not going to learn everything you need to know in one shot. It take's time. Your question is about hot inject you MyObject into your resource, and the duplicate link has the answer. If you have a different more specific problem, please as a new question. – Paul Samsotha Feb 04 '16 at 00:24
  • @peeskillet Can you at least correct your comment >>The first link you provided is an example of the latter, and the first link is an example of the former Which link is which? – ksl Feb 04 '16 at 10:35
  • Sorry, I noticed that, but I thought you may figure it out. You provided two links in your post. My comment should be corrected _"The second link you provided is an example of the latter, "_ – Paul Samsotha Feb 04 '16 at 10:40
  • First of all you need to create a beans.xml file under WEB-INF directory of your project. Only then container will scan through your classes for beans. Here is a [simple example](http://buraktas.com/java-cdi-dependency-injection-example/) for CDI. Here is a reference [article](https://www.ibm.com/developerworks/websphere/techjournal/1301_stephen/1301_stephen.html) – TMtech Nov 01 '16 at 04:50

0 Answers0