Spring-boot中servlet异步摄错

来源:1-8 异步非阻塞 Servlet 代码示例

Amy楠

2019-04-30

@WebServlet(urlPatterns = "/my/servlet",
        asyncSupported = true)
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        AsyncContext asyncContext = req.getAsyncContext();

        asyncContext.start(()->{
            try {
                resp.getWriter().println("helloWorld");

            } catch (IOException e) {
                e.printStackTrace();
            }
            asyncContext.complete();
        });
    }
}

There was an unexpected error (type=Internal Server Error, status=500).
It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)

写回答

1回答

Amy楠

提问者

2019-04-30

好吧,我自己写错了,

AsyncContext asyncContext = req.startAsync();

0
0

Spring Boot2.0深度实践 核心原理拆解+源码分析

系统化剖析Spring Boot 2.0,掌握技术原理,学习源码阅读技巧

2904 学习 · 213 问题

查看课程