将企业bean注入JAX-RS 2.0 REST服务时出现NullPointerException
我正试图在我的一个RESTful服务中注入和EJB3.1。我遵循了帖子:并尝试了除构建注入提供者之外的所有选项。我正在尝试的当前解决方案使用了将企业bean注入JAX-RS 2.0 REST服务时出现NullPointerException,rest,jakarta-ee,ejb,jax-rs,wildfly,Rest,Jakarta Ee,Ejb,Jax Rs,Wildfly,我正试图在我的一个RESTful服务中注入和EJB3.1。我遵循了帖子:并尝试了除构建注入提供者之外的所有选项。我正在尝试的当前解决方案使用了@RequestScoped和@Inject的组合,但是我的injected bean变量仍然是null。我在/WEB-INF文件夹中有一个beans.xml 如何将EJB注入REST服务类 用户服务 @Local @Path("user/v1") @Produces(MediaType.APPLICATION_JSON) public class
@RequestScoped
和@Inject
的组合,但是我的injected bean变量仍然是null
。我在/WEB-INF
文件夹中有一个beans.xml
如何将EJB注入REST服务类
用户服务
@Local
@Path("user/v1")
@Produces(MediaType.APPLICATION_JSON)
public class UserServiceV1 implements SystemLogger {
@Inject
private ApplicationBean appBean;
@GET
@Path("pingbean")
@Produces(MediaType.APPLICATION_JSON)
public Response pingAppBean() {
if(appBean == null) {
return Response.status(Response.Status.UNAUTHORIZED).entity("{\"faild\": \"App bean is null\"}").build();
}
String message = appBean.getHello();
return Response.status(Response.Status.OK)
.entity(message)
.build();
}
}
应用程序bean
SystemHandler
驻留在jar模块中,是一个具有业务逻辑的标准类
@Stateless
@Local
public class ApplicationBean implements ApplicationBeanLocal {
@Override
public String getHello() {
return "Hello from ApplicationBean";
};
}
JAX-RS配置
@ApplicationPath("service")
public class ApplicationService extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet<>();
resources.add(UserServiceV1.class);
resources.add(ApplicationBean.class);
resources.add(CorsFilterProvider.class);
return resources;
}
}
Resteasy/JAX-RS
我已经根据添加了一个resteasy的CDI实现
org.jboss.resteasy
resteasy jaxrs
3.0.14.1最终版本
假如
org.jboss.resteasy
resteasy cdi
3.0.14.1最终版本
假如
编辑:问题和标题中更改的代码详细信息EJB3.1意味着Java EE 6容器实现。 JavaEE6意味着JAX-RS1.1 JAX-RS 1.1仅用于支持企业java bean的@EJB注入
Java EE 7实现中提供的JAX-RS 2.0支持@Inject for EJB。正如Steve C所回答的,在JAX-RS 1.1中插入EJB的正确方法是使用javax.EJB.EJB注释。REST服务也必须是EJB才能工作。因此,您必须使用javax.ejb.Stateless注释,而不是javax.enterprise.context.requestscope 最终结果如下:
@Stateless
@Path("user/v1")
public class UserServiceV1 implements SystemLogger {
@EJB
private ApplicationBean appBean;
//etc.
}
编辑
您更新的代码未部署。要么让ApplicationBean
实现ApplicationBeanLocal
并注入接口,要么不实现它并直接注入类。纠正了这个错误后,我成功地运行了您的示例
另外,在ApplicationService
中,您不需要添加ApplicationBean.class
。您仅在这些REST根资源和功能提供程序中注册@Local
在UserServiceV1
类中也是不必要的,它不是EJB
此外,它是beans.xml
,而不是bean.xml
(但是从CDI1.1开始,这个文件不再是必需的)
请参见下面的我的测试代码:
jar模块的依赖项:
<dependencies>
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>3.2</version>
</dependency>
</dependencies>
ApplicationBean.java
:
public interface ApplicationBeanLocal {
String getHello();
}
@Stateless
@Local
public class ApplicationBean implements ApplicationBeanLocal {
@Override
public String getHello() {
return "Hello from ApplicationBean";
}
}
@ApplicationPath("service")
public class TestApplication extends Application {
private final Set<Class<?>> resources = new HashSet<>();
public TestApplication() {
resources.add(UserServiceV1.class);
}
@Override
public Set<Class<?>> getClasses() {
return resources;
}
}
pom.xml
JAX-RS应用程序的依赖项:
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.14.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-cdi</artifactId>
<version>3.0.14.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<!-- The jar-module containing ApplicationBean and ApplicationBeanLocal -->
<dependency>
<groupId>test</groupId>
<artifactId>testjar</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
UserServiceV1.java
@Path("user/v1")
@RequestScoped
@Produces(MediaType.APPLICATION_JSON)
public class UserServiceV1 {
@Inject // Note that I'm referencing the interface, not the implementation
private ApplicationBeanLocal appBean;
@GET
@Path("pingbean")
@Produces(MediaType.APPLICATION_JSON)
public Response pingAppBean() {
final String message = appBean.getHello();
return Response.status(Response.Status.OK).entity(message).build();
}
}
我使用@EJB将bean注入到我用JAX-RS1.1完成的其他项目中。但是我当前的项目使用JAX-RS2.0,请参阅resteasy jaxrs依赖项。明天将尝试解决此问题,欢迎任何输入。您使用的应用程序服务器和版本是什么?我假设JBoss EAP或Wildfly,因为您声明了RestEasy而不是Jersey作为依赖项。如果您想使用该RestEasy版本,而不是JBoss提供的版本(可能是较旧的、符合JAX-RS 1.1的版本),您还必须将模块从部署中排除。请参阅:您还必须从依赖项中删除
提供的
,以便将其打包到应用程序中。我正在运行Wildfly 10.0.final,它附带resteasy JAXR,这就是为什么我要这么做的原因。当war文件已经包含在应用服务器中时,在war文件中包含单独的库没有意义。
@ApplicationPath("service")
public class TestApplication extends Application {
private final Set<Class<?>> resources = new HashSet<>();
public TestApplication() {
resources.add(UserServiceV1.class);
}
@Override
public Set<Class<?>> getClasses() {
return resources;
}
}
@Path("user/v1")
@RequestScoped
@Produces(MediaType.APPLICATION_JSON)
public class UserServiceV1 {
@Inject // Note that I'm referencing the interface, not the implementation
private ApplicationBeanLocal appBean;
@GET
@Path("pingbean")
@Produces(MediaType.APPLICATION_JSON)
public Response pingAppBean() {
final String message = appBean.getHello();
return Response.status(Response.Status.OK).entity(message).build();
}
}