与嵌入式tomcat一起运行的webflux的异步错误

时间:2018-09-27 07:08:45

标签: java spring tomcat asynchronous spring-webflux

我正在学习webflux和tomcat9。我用单一方法(API)制作了一个简单的应用程序。 我成功地使用嵌入式tomcat运行了它。但是当我请求一个方法时,我会出错。

这是我的代码:

    NewsRepository newsRepository = new DummyNewsRepository();
    NewsHandler newsHandler = new NewsHandler(newsRepository);
    RouterFunction<ServerResponse> newsRoute = route(GET("/news"), newsHandler::getNews);

    HttpHandler httpHandler = RouterFunctions.toHttpHandler(newsRoute);

    Servlet servlet = new ServletHttpHandlerAdapter(httpHandler);


    Tomcat server = new Tomcat();
    File base = new File(System.getProperty("java.io.tmpdir"));
    Context rootContext = server.addContext("", base.getAbsolutePath());
    Tomcat.addServlet(rootContext, "main", servlet);
    rootContext.addServletMappingDecoded("/", "main");
    server.setHostname("localhost");
    server.setPort(8080);
    server.getConnector();

    server.start();
    server.getServer().await();

错误:

    Sep 27, 2018 11:56:25 AM org.apache.catalina.connector.Request startAsync
WARNING: Unable to start async because the following classes in the processing chain do not support async [org.springframework.http.server.reactive.ServletHttpHandlerAdapter]
java.lang.IllegalStateException: A filter or servlet of the current chain does not support asynchronous operations.
    at org.apache.catalina.connector.Request.startAsync(Request.java:1665)
    at org.apache.catalina.connector.Request.startAsync(Request.java:1657)
    at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1043)
    at org.springframework.http.server.reactive.ServletHttpHandlerAdapter.service(ServletHttpHandlerAdapter.java:164)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:2)

这最初是由M. Deinum在评论中发布的:

ServletHttpHandlerAdapter未配置用于异步处理。 您必须在包装器上将asyncSupported属性设置为trueTomcat.addServlet返回。但是我强烈建议 使用Spring Boot而不是自己尝试