0

I am making a simple RESTful service using JAVAX RS and EJB in order to create a singleton object as a global variable.

I initiated the service as follows:

    import org.glassfish.grizzly.http.server.HttpServer;
    import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
    import org.glassfish.jersey.server.ResourceConfig;

    final ResourceConfig rc = new ResourceConfig().packages("service_folder");
    GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);

I am currently just testing the @Stateless example.

Firstly, I create a class in a random folder, and the content as follows:

    import javax.ejb.Stateless;
    import java.util.Date;


    @Stateless
    public class Service {
        public Date getCurrentDate(){
            return new Date();
        }
    }

Then I create the resource file in the service_folder mentioned in the very beginning:

    @Stateless
    @Path("current")
    public class ServiceFacade {

        @EJB
        Service service;

        @GET
        public String getDate(){
            return service.getCurrentDate().toString();
        }

    }

The current situation is that whenever I access BASE_URI/current, grizzly simply throw an error, and the reason is because service in getDate() is null.

My guess is that during the grizzly init., the Service class bean isn't really registered yet.

Please let me know where did I do wrong, thanks!

xardbaiz
  • 684
  • 7
  • 17
Lumin
  • 111
  • 1
  • 1
  • 5
  • Duplicate question [Inject an EJB into JAX-RS (RESTful service)](http://stackoverflow.com/questions/3027834/inject-an-ejb-into-jax-rs-restful-service) and/or [Dependency injection with Jersey 2.0](http://stackoverflow.com/questions/16216759/dependency-injection-with-jersey-2-0) – zyexal Apr 07 '16 at 00:33
  • @zyexal But OP followed option #2 in that answer and it's still not working? – Brett Kail Apr 07 '16 at 07:03
  • Nope... However I was able to use @Injection to make the singleton instance work. I don't have to use EJB at all. Thanks for the help tho. – Lumin Apr 07 '16 at 18:55
  • Does this answer your question? [Inject an EJB into JAX-RS (RESTful service)](https://stackoverflow.com/questions/3027834/inject-an-ejb-into-jax-rs-restful-service) – BalusC Apr 16 '20 at 01:19

0 Answers0