Java 将EJB注入RESTeasy Web服务
我目前在将Java 将EJB注入RESTeasy Web服务,java,resteasy,wildfly,Java,Resteasy,Wildfly,我目前在将@无状态的-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("
@无状态的-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;
}
}
公共类SalomeApplication扩展应用程序
{
private Set singleton=new HashSet();
private Set>getClasses()
{
将此文件返回。空;
}
公共集getSingleton()
{
归还这个。单身人士;
}
}
我正在使用org.jboss.resteasy:resteasy-jaxrs:3.0.11.Final
,Wildfly
和应用服务器。我还尝试使用Inject
和RequestScoped
来代替,但这两种方法都不起作用。事件资源的实例是由您创建的,因为您没有设置EventService
引用,它当然必须是null
。
您还将此实例注册为Singleton,因此您将始终准确地获得此实例
如果将EventResource.class
添加到类集中,RESTeasy将负责创建实例和管理依赖项。如果您喜欢使用单例,您应该使用自动扫描功能。在Wildfly上,这是默认启用的,因此您只需删除SalomeApplication
的内容即可