Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将EJB注入RESTeasy Web服务_Java_Resteasy_Wildfly - Fatal编程技术网

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
的内容即可