Jersey Inject RequestScoped IllegalStateException

时间:2016-09-03 23:41:01

标签: hibernate jersey-2.0 hk2

已经在这个工作了几个小时,似乎没有任何工作。这是泽西岛2.23.2。我的想法是,我想将一个基于Hibernate Session的请求注入Jersey提供程序,以便在我的REST API中使用。相反,我得到了这个:

javax.servlet.ServletException: A MultiException has 2 exceptions.  They are:
1. java.lang.IllegalStateException: Could not find an active context for javax.enterprise.context.RequestScoped
2. java.lang.IllegalStateException: While attempting to create a service for SystemDescriptor(
    implementation=com.pixmoto.api.server.SFFactory
    contracts={org.hibernate.Session}
    scope=javax.enterprise.context.RequestScoped
    qualifiers={}
    descriptorType=PROVIDE_METHOD
    descriptorVisibility=NORMAL
    metadata=
    rank=0
    loader=org.glassfish.hk2.utilities.binding.AbstractBinder$2@6e9becd5
    proxiable=null
    proxyForSameScope=null
    analysisName=null
    id=129
    locatorId=0
    identityHashCode=1082491454
    reified=true) in scope javax.enterprise.context.RequestScoped an error occured while locating the context

这是我的代码:

public class JerseyResourceConfig extends ResourceConfig {

    public JerseyResourceConfig() {
        packages(this.getClass().getPackage().getName() + ";org.codehaus.jackson.jaxrs");

        register(new AbstractBinder(){
            @Override
            protected void configure() {
                bindFactory(SFFactory.class)
                .to(Session.class)
                .in(RequestScoped.class);
            }
        });
    }
}

public class App {

    private static final Logger logger = LogManager.getLogger(App.class);

    public static void main(String[] args) throws Exception {

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

        Server jettyServer = new Server(8080);

        Slf4jRequestLog requestLog = new Slf4jRequestLog();
        requestLog.start();
        jettyServer.setRequestLog(requestLog);

        ClassList classlist = ClassList.setServerDefault(jettyServer);
        classlist.addAfter(
                "org.eclipse.jetty.webapp.FragmentConfiguration", 
                "org.eclipse.jetty.plus.webapp.EnvConfiguration", 
                "org.eclipse.jetty.plus.webapp.PlusConfiguration");

        jettyServer.setHandler(context);

        JerseyResourceConfig config = new JerseyResourceConfig();
        ServletHolder servlet = new ServletHolder(new ServletContainer(config));
        context.addServlet(servlet, "/*");

        try {
            jettyServer.start();
            jettyServer.join();
        } catch (Exception ex) {
            logger.error(ex);
            throw ex;
        } finally {
            jettyServer.destroy();
        }
    }
}

@Path("/api/users")
@Produces(MediaType.APPLICATION_JSON)
public class UserProvider {

    @Inject
    private javax.inject.Provider<Session> session;

    @GET
    public List<User> getUsers() {

        Session s = session.get();
        List<User> users = s.createQuery( "from User", User.class).getResultList();
        return users;
    }
}

public class SFFactory implements Factory<Session> {

    private final SessionFactory sessionFactory;

    public SFFactory() {

     this.sessionFactory = new Configuration()
                .configure() // configures settings from hibernate.cfg.xml
                .buildSessionFactory();
    }

    @Override
    public Session provide() {
        return sessionFactory.openSession();
    }

    @Override
    public void dispose(Session session) {
        if (session.isOpen()) {
            session.close();
        }
    }
}

我已经尝试了几百种排列。不知疲倦地搜索网络。只要我在没有分辨率的情况下看着它,我就知道它必须是非常简单的东西。

当我在AbstractBinder配置方法中指定Singleton而不是RequestScoped时,我可以让它绑定,但这不是我想要的。我想在一个SessionFactory的每个请求上都有一个新的Session。

我没有更多的头发了。任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:4)

你使用了错误的RequestScoped(你正在使用CDI)。你应该使用特定的泽西岛。