如何在使用 Google guice ServletModule 在代码中加载 servlet 时在 Jetty 9.3 中使 _asyncSupported = true

How to make _asyncSupported = true in Jetty 9.3 while servlet is loaded in code with Google guice ServletModule

我正在尝试将 Jetty 从 8 升级到 9.3。由于 _asyncSupported 的默认值变为 false,因此将显示以下错误。

java.lang.IllegalStateException: !asyncSupported: stackDumperFilter at org.eclipse.jetty.server.Request.startAsync(Request.java:2248) at org.eclipse.jetty.continuation.Servlet3Continuation.suspend(Servlet3Continuation.java:188) at org.eclipse.jetty.servlets.ProxyServlet.service(ProxyServlet.java:659)

通过Googleguice的ServletModule在代码中加载servlet的方式如下

public class ProxyModule extends ServletModule {
    @Override
    protected void configureServlets() {
        serve("/someurl/*").with(ProxyServlet.class);
    }
}

@Singleton
public static class ProxyServlet extends SuperHttpProxyServlet {

    @Inject
    public ProxyServlet(@Named("something.proxy") Transparent proxy) {
            super(proxy);
        }
}

Jetty 9升级后,将采用默认值_asyncSupported,变为false。所以它会在 jetty 库文件中给出以下原因的异常 (Package : org.eclipse.jetty.server).

public AsyncContext startAsync() throws IllegalStateException
    {
        if (!_asyncSupported)
            throw new IllegalStateException("!asyncSupported");
        _async.startAsync();
        return _async;
    }

那么当 ProxyServlet 被 Google Guice 的 ServletModule 调用时如何使它成为 asyncSupported (true)? 我试过注释,但它不起作用。

@WebServlet(urlPatterns={"/someurl/*"}, asyncSupported=true, loadOnStartup = 1)
@Singleton
public static class ProxyServlet extends SuperHttpProxyServlet {

    @Inject
    public ProxyServlet(@Named("something.proxy") Transparent proxy) {
                super(proxy);
            }
    }

但是由于同样的错误而失败了。

java.lang.IllegalStateException: !asyncSupported: stackDumperFilter at org.eclipse.jetty.server.Request.startAsync(Request.java:2248) at

将您的 stackDumpFilter 设置为 asyncSupported=true

经验法则是,如果过滤器链中的任何内容(所有过滤器 + servlet)使用异步,则所有这些过滤器和 servlet 都必须设置为 asyncSupported=true。