これは何時間も作業していましたが、何も動作していないようです。ジャージー2.23.2です。私は、REST APIで使用するためにJerseyプロバイダにリクエストベースのHibernate Sessionを注入したいと考えています。代わりに、私はこの取得:ジャージインジェクションRequestScoped IllegalStateException
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
[email protected]ecd5
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のconfigureメソッドでRequestScopedではなくSingletonを指定するとバインドできますが、これは私が望むものではありません。私は単一のSessionFactoryのすべての要求に対して新しいSessionを望みます。
私は引き出す髪がもうありません。どんな助けも非常に高く評価されます。
私はこの自分自身に答えるためにここに戻って来ていました。あなたはまさに正しいです。私はそれが単純でなければならないことを知っていた。今日は5時間もかかっていませんでした。見ていただきありがとうございます。 – ptoinson