java—当SpringBoot中有同步和异步处理器时,如何获取同步进程的响应体

huwehgph  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(273)

我想截取并得到同步处理的响应体。如果我尝试这样做,我的控制器-无论是同步和异步处理器正常工作,但我没有收到来自else子句同步响应

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper((HttpServletResponse) response);
    chain.doFilter(request, response);

    if (request.isAsyncStarted()) {
        //Some code
    } else {
        byte[] responseArray = responseWrapper.getContentAsByteArray();
        String responseString = new String(responseArray, responseWrapper.getCharacterEncoding());
        System.out.println("response = " + responseString);
        responseWrapper.copyBodyToResponse();
    }

    ThreadContext.clearMap();
}

如果我尝试这样做,我会在else子句中收到响应主体,但异步进程不会释放http线程

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper((HttpServletResponse) response);

chain.doFilter(request, responseWrapper);

if (request.isAsyncStarted()) {
//Some code
} else {
byte[] responseArray = responseWrapper.getContentAsByteArray();
String responseString = new String(responseArray, responseWrapper.getCharacterEncoding());
System.out.println("response = " + responseString);
responseWrapper.copyBodyToResponse();
}
ThreadContext.clearMap();
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题