RestEasy @EJB returning zero

I have a jax-rs service implemented using RestEasy, I use Jboss as7 and have implemented signleton ejb with @Signleton annotation. The icon starts when the server starts (@startup), and I would like to enter it in jax-rs using @EJB. The problem is that the class is always null, and I'm starting to be mistaken, because in every tutorial I saw, this is how they introduce ejb. Should I use any special XML file? what am I doing wrong?

@Path("/")
public class Service extends Application{
@EJB
private GlobalStore store;
public Service(){
fromejbstore = store.getSentiment();//null pointer is thrown
}
}

The ejb signal is:

@Singleton
@Startup
public class GlobalStore {
 Sentiment sentiment;
 @PostConstruct
public void initialize() {
 //do something
}
public Sentiment getSentiment(){return sentiment;}
}
+3
source share
6 answers

, Rest EJB. null, @EJB. @Stateless , . , @Stateless . .

+1

, :

    `try{
        InitialContext ctx=new InitialContext();
        localRef = (myEjb) ctx.lookup("java:global/appName/EjbName");
       }catch(NamingException ne){
            System.out.println("\n[MyRestService]NamingException: "+ne);
            ne.printStackTrace();
       }`

, @EJB , , - RestEasy.

+2

AFAIK, @EJB POJO - Service bean ( @Stateless ), GlobalStore

+2

, JAX-RS. , Application @ApplicationPath, Java EE 6 " XML" JAX-RS.

@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
 /* class body intentionally left blank */
}

Service , JaxRsActivator, Application:

@Path("/test")
public class Service
{
  @EJB private GlobalStore store;

  @GET @Path("/test")
  public String getTime()
  {
   boolean test = (store==null);
   return "Test: "+test;
  }
}

http://<host>/<context>/rest/test/test.

0

. @Stateless , @Local, , , bean, @EJB . , , Websphere Liberty Profile, - beans.xml WEB-INF, NPE cdi bean.

@Stateless
public class ServiceImpl implements Service {
    public void doSomething() {
        System.out.println("I did something");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("I was just constructed");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("Arrgg I am being destroyed");
    }
}

@Local
public interface Service {
    public void doSomething();
}


public class Client {

    @EBJ Service service;

    public void someMethod() {
        service.doSomething();
    }
}

../WEB-INF/beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
  http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

post bean, , , .

0

beans.xml web-inf:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

extends Application:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import java.util.HashSet;
import java.util.Set;

@ApplicationPath("/rest")

public class YourApplication extends Application
{
   HashSet<Object> singletons = new HashSet<Object>();

   public YourApplication()
   {
      getClasses().add(yourOwnClass.class);
   }

   @Override
   public Set<Class<?>> getClasses()
   {
      HashSet<Class<?>> set = new HashSet<Class<?>>();
      return set;
   }

    enter code here

   @Override
   public Set<Object> getSingletons()
   {
      return singletons;  
   }
}
-2
source

All Articles