错误 WELD-001303:范围类型 jakarta.enterprise.context.SessionScoped 没有活动上下文

error WELD-001303: No active contexts for scope type jakarta.enterprise.context.SessionScoped

在 tomcat9,Java8 -> 没问题。

我要升级到 tomcat10,Java16

<dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se-core</artifactId>
    <version>4.0.0.Final</version>
</dependency>
<dependency>
    <groupId>jakarta.enterprise</groupId>
    <artifactId>jakarta.enterprise.cdi-api</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>jakarta.inject</groupId>
    <artifactId>jakarta.inject-api</artifactId>
    <version>2.0.0</version>
</dependency>

这是我的beans.xml

<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_3_0.xsd"
        version="3.0"
        bean-discovery-mode="all">
</beans>

Session.java:

import jakarta.enterprise.context.SessionScoped;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.ws.rs.ForbiddenException;
import jakarta.ws.rs.core.SecurityContext;

/**
 * セッションインターフェース
 */
@SuppressWarnings("serial")
@SessionScoped
public class Session implements Principal, SecurityContext, Serializable, Map<String, Object> {

    /**
     * インスタンスを取得
     * 
     * @return インスタンス
     */
    public static Session current() {
        try {
            return CDI.current().select(Session.class).get();
        } catch (Exception e) {
            return new Session();
        }
    }

Main.java:

 @Inject private Session session;
 
 @GET @Path("index.html") public Object index() {
     if (session.getCategoryCode() != null) { 
         return Response.temporaryRedirect(URI.create("menu.html")).build();
     }
     return Tool.responseTemplate(template, "index", Notice.html(LocalDateTime.now().format(Tool.yyyymmddhh))); }

当我在 If (session.getCategoryCode()... 会话变量总是显示“com.sun.jdi.InvocationException:调用方法时目标虚拟机发生异常。”

这是启动时的异常错误

2021/06/22 09:35:13.916 WARNING [app.inject.MapperOfInternalServerError.toResponse]  org.jboss.weld.contexts.ContextNotActiveException: WELD-001303: No active contexts for scope type jakarta.enterprise.context.SessionScoped
    at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:647)
    at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:89)
    at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:164)
    at org.jboss.weld.bean.ContextualInstance.getIfExists(ContextualInstance.java:63)
    at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:87)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:131)
    at app.inject.Session$Proxy$_$$_WeldClientProxy.getCategoryCode(Unknown Source)
    at app.controller.Main.index(Main.java:90)
    at app.controller.Main$Proxy$_$$_WeldClientProxy.index(Unknown Source)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static[=15=](ResourceMethodInvocationHandlerFactory.java:52)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.run(AbstractJavaResourceMethodDispatcher.java:124)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ObjectOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:192)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:475)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:397)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:81)
    at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:255)
    at org.glassfish.jersey.internal.Errors.call(Errors.java:248)
    at org.glassfish.jersey.internal.Errors.call(Errors.java:244)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:223)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:870)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1696)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:831)

当我在 org.jboss.weld.bean.proxy.ClientProxyProvider

调试时
public <T> T getClientProxy(final Bean<T> bean, Type requestedType) {
    // let's first try to use the proxy that implements all the bean types
    T proxy = beanTypeClosureProxyPool.getCastValue(bean);
    if (proxy == BEAN_NOT_PROXYABLE_MARKER) {
        /*
         *  the bean may have a type that is not proxyable - this is not a problem as long as the unproxyable
         *  type is not in the type closure of the requested type
         *  https://issues.jboss.org/browse/WELD-1052
         */
        proxy = requestedTypeClosureProxyPool.getCastValue(new RequestedTypeHolder(requestedType, bean));
        if (proxy == BEAN_NOT_PROXYABLE_MARKER) {
            throw Proxies.getUnproxyableTypeException(requestedType, services());
        }
    }
    BeanLogger.LOG.lookedUpClientProxy(proxy.getClass(), bean);
    return proxy;
}

requestedType 是 app.inject.Session(我的 SessionScoped class) 在 T proxy = beanTypeClosureProxyPool.getCastValue(bean); 它总是显示“com.sun.jdi.InvocationException:目标 VM 发生异常调用方法 。”

接下来,我创建 Test.java 并仅在 Main.java

注入它
@SessionScoped
public class Test implements Serializable{
    private static final long serialVersionUID = 1L;
    private String id;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}

Main.java

    @Inject
    private Test test;
    
    @GET
    @Path("index.html")
    public Object index() {
        if (session.getCategoryCode() != null) { 
            return Response.temporaryRedirect(URI.create("menu.html")).build();
        }
        return Tool.responseTemplate(template, "index", Notice.html(LocalDateTime.now().format(Tool.yyyymmddhh)));
    }

所以它显示了同样的错误。

ApplicationScoped、RequestScoped、Dependent...=> 没问题。 只有 SessionScoped 错误。

如何解决?

问题:jersey-cdi2-se 在 servlet 环境中不工作。

更改为 jersey-cdi1x-servlet => 完成!!!!!!!!!!!!!!!!

参考: