Java – how to get the response body in zuul post filter?
•
Java
How to read the response body when zuul is used as a proxy in the post filter?
I try to call the code like this:
@Component public class PostFilter extends ZuulFilter { private static final Logger log = LoggerFactory.getLogger(PostFilter.class); @Override public String filterType() { return "post"; } @Override public int filterOrder() { return 2000; } @Override public boolean shouldFilter() { return true; } @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); ctx.getResponseBody(); // null // cant't do this,cause input stream is used later in other filters and I got InputStream Closed exception // GZIPInputStream gzipInputStream = new GZIPInputStream(stream); return null; } }
Solution
I succeeded in overcoming this The solution consists of four steps:
>Set CTX Getresponsedatastream() reads in bytearrayoutputstream > copy OutputStream to 2 inputstreams. > Use one of them for custom purposes. > Use the second reassignment to the context: context setResponseBody(inputStream)
>Reading a stream from point 1 will cause the stream to be unreadable again, so you pass a new stream that has not been read
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码