根据Servlet 3.1容器上的documentation ,ServletHttpHandlerAdapter可以注册为Servlet并可以支持nio。 我需要将请求转发到如下所示的其他资源
asyncContext.getRequest().getServletContext().getRequestDispatcher("/OtherResource")
.forward(asyncContext.getRequest(), asyncContext.getResponse());
在致电时我遇到了错误。
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:624)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:211)
at com.nucleus.OtherServlet.doGet(OtherServlet.java:30)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:728)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:470)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:395)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:316)
at com.nucleus.AsyncRequestProcessor.run(AsyncRequestProcessor.java:55)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
当spring创建org.springframework.http.server.reactive.ServletServerHttpResponse并最初调用response.getOutputStream()时,这是正确的,现在如何支持请求转发?
该问题还登录了spring jira进行讨论-Link
答案 0 :(得分:0)
借助于ServletHttpHandlerAdapter
,Spring WebFlux可以部署在Servlet 3.1容器上-但这并不意味着您可以在该应用程序中使用Servlet特定的API。
首先,一旦使用了异步I / O Servlet功能,就不允许将它们与Servlet API(see this presentation about Servlet 3.1 async IO)上阻止I / O调用混合使用。
因此,一般而言,您永远不要尝试从ServerWebExchange
获取特定于Servlet的对象,而应针对Spring WebFlux中这种新的HTTP基础进行编码。
There is an issue about supporting forwarding at the WebFlux level,但我不确定这最终能否解决您的问题。我不了解您遇到的问题的具体细节,但是Spring WebFlux已经支持使用阻塞I / O(see Freemarker support here)的模板引擎。
如果您要支持的模板引擎与Servlet绑定,那么您将无法获得适当的WebFlux支持,因为可以在非Servlet环境中运行Spring WebFlux应用程序。