1
votes

I try to build a Java EE 7 app with Rest services and EJB injection. I created a multi module maven project which I deploy on Glassfish 4. My final EAR contain a JAR with my EJBs, with for example my Rest services definitions :

@Stateless
@Path("countries")
public class CountryRest {

   //@EJB 
   //StockService stockService;

   @GET
   public Response getCountries() {
      //stockService.getAll(); --> firing a NPE, stockService is Null
      return Response.ok().build();
   }
}

@Stateless
@Remote(IStockService.class)
public class StockService implements IStockService {

    @Override
    public List<Stock> getAllStock() {
        return new ArrayList<Stock>();
    }
}

When I deploy my app, I see the following logs which seems ok. Even if I wonder why it defines "java:global" JNDI since by default @Stateless EJB is @Local :

Portable JNDI names for EJB CountryRest: [java:global/GeoData-ear/GeoData-ejb-1.0-SNAPSHOT/CountryRest, java:global/GeoData-ear/GeoData-ejb-1.0-SNAPSHOT/CountryRest!com.tomahim.geodata.rest.CountryRest]
Portable JNDI names for EJB StockService: [java:global/GeoData-ear/GeoData-ejb-1.0-SNAPSHOT/StockService, java:global/GeoData-ear/GeoData-ejb-1.0-SNAPSHOT/StockService!com.tomahim.geodata.services.IStockService]

Then when I'm doing a GET on /rest/countries, the status is 200 as expected but I have a NameNotFoundException / NamingException :

Avertissement: An instance of EJB class, com.tomahim.geodata.rest.CountryRest, could not be looked up using simple form name. Attempting to look up using the fully-qualified form name.
javax.naming.NamingException: Lookup failed for 'java:module/CountryRest' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: No object bound to name java:module/CountryRest]

I see that the lookup for "java:module/ContryRest" is not matching "java:global/.../CountryRest" but what am I doing wrong ?

EDIT 1 : I was able to make @Ejb injection work by placing my Rest definitions ans EJBs in my webapp maven module deploy as a WAR. So it seems that the problem occur only when I deploy my EJB in a JAR. Any idea ? What could be the difference between JAR and WAR deployment ?

1

1 Answers

3
votes

It is required by the JAX-RS spec that all REST endpoints must live within your WAR file. Do you really need an EAR file?