【发布时间】:2015-01-03 03:50:24
【问题描述】:
我尝试使用 Rest 服务和 EJB 注入构建 Java EE 7 应用程序。 我创建了一个在 Glassfish 4 上部署的多模块 maven 项目。我的最终 EAR 包含一个带有我的 EJB 的 JAR,例如我的 Rest 服务定义:
@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>();
}
}
当我部署我的应用程序时,我看到以下日志似乎没问题。即使我想知道为什么它定义了“java:global”JNDI,因为默认情况下@Stateless EJB 是@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]
然后当我在 /rest/countries 上执行 GET 时,状态为 200,但我有一个 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]
我看到“java:module/ContryRest”的查找与“java:global/.../CountryRest”不匹配,但我做错了什么?
编辑 1:通过将我的 Rest 定义和 EJB 放置在我的 webapp maven 模块部署为 WAR,我能够使 @Ejb 注入工作。所以似乎只有当我在 JAR 中部署 EJB 时才会出现问题。任何的想法 ? JAR 和 WAR 部署有什么区别?
【问题讨论】:
标签: rest glassfish jax-rs ejb-3.0