将 EJB 注入 RESTeasy web 服务
Inject EJB into RESTeasy webservice
我目前在将 @Stateless
-EJB 注入我的 RESTeasy 实现的 Web 服务时遇到问题:
资源:
import javax.ejb.EJB;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/rest/events")
public class EventResource
{
@EJB
EventService eventService;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAllEvents()
{
System.out.println(eventService);
return Response.ok().build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}")
public Response getEventById(@PathParam("id") String id)
{
System.out.println(id);
int intId = Integer.parseInt(id);
Event e = eventService.getById(intId);
System.out.println(e);
return Response.ok(e).build();
}
服务:
@Stateless
public class EventService
{
...
}
申请:
public class SalomeApplication extends Application
{
private Set<Object> singletons = new HashSet();
private Set<Class<?>> empty = new HashSet();
public SalomeApplication()
{
this.singletons.add(new EventResource());
}
public Set<Class<?>> getClasses()
{
return this.empty;
}
public Set<Object> getSingletons()
{
return this.singletons;
}
}
我正在使用 org.jboss.resteasy:resteasy-jaxrs:3.0.11.Final
、Wildfly
和应用程序服务器。我也尝试使用 Inject
和 RequestScoped
代替 - 也不起作用。
EventResource
的实例是由您创建的,因为您没有设置 EventService
引用,所以它当然必须是 null
。
您还将此实例注册为 Singleton,因此您将始终准确地获得此实例。
如果您将 EventResource.class
添加到 类 的集合中,RESTeasy 将负责创建实例和管理依赖项。如果您更喜欢使用 Singletons,您应该使用自动扫描功能。在 Wildfly 上,这是默认启用的,因此您需要做的就是删除 SalomeApplication
.
的内容
我目前在将 @Stateless
-EJB 注入我的 RESTeasy 实现的 Web 服务时遇到问题:
资源:
import javax.ejb.EJB;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/rest/events")
public class EventResource
{
@EJB
EventService eventService;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAllEvents()
{
System.out.println(eventService);
return Response.ok().build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}")
public Response getEventById(@PathParam("id") String id)
{
System.out.println(id);
int intId = Integer.parseInt(id);
Event e = eventService.getById(intId);
System.out.println(e);
return Response.ok(e).build();
}
服务:
@Stateless
public class EventService
{
...
}
申请:
public class SalomeApplication extends Application
{
private Set<Object> singletons = new HashSet();
private Set<Class<?>> empty = new HashSet();
public SalomeApplication()
{
this.singletons.add(new EventResource());
}
public Set<Class<?>> getClasses()
{
return this.empty;
}
public Set<Object> getSingletons()
{
return this.singletons;
}
}
我正在使用 org.jboss.resteasy:resteasy-jaxrs:3.0.11.Final
、Wildfly
和应用程序服务器。我也尝试使用 Inject
和 RequestScoped
代替 - 也不起作用。
EventResource
的实例是由您创建的,因为您没有设置 EventService
引用,所以它当然必须是 null
。
您还将此实例注册为 Singleton,因此您将始终准确地获得此实例。
如果您将 EventResource.class
添加到 类 的集合中,RESTeasy 将负责创建实例和管理依赖项。如果您更喜欢使用 Singletons,您应该使用自动扫描功能。在 Wildfly 上,这是默认启用的,因此您需要做的就是删除 SalomeApplication
.