public class HttpResponseHeadersHandler extends ChannelInboundHandlerAdapter {
private final HttpHeaders httpHeaders;
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpResponse && !HttpStatus.resolve(((HttpResponse) msg).status().pre()).is1xxInformational()) { HttpHeaders headers = ((HttpResponse) msg).headers(); httpHeaders.forEach(e -> { log.warn("Modifying {} from: {} to: {}.", e.getKey(), headers.get(e.getKey()), e.getValue()); headers.set(e.getKey(), e.getValue()); }); } ctx.fireChannelRead(msg); }}
然后,我创建一个和
ClientHttpConnector一起使用
WebClient并
afterNettyContextInit添加处理程序:
ctx.addHandlerLast(new ReadTimeoutHandler(readTimeoutMillis, TimeUnit.MILLISECONDS));ctx.addHandlerLast(new Slf4JLoggingHandler());if (forceDecompression) { io.netty.handler.prec.http.HttpHeaders httpHeaders = new ReadonlyHttpHeaders( true, CONTENT_ENCODING, GZIP, CONTENT_TYPE, APPLICATION_JSON ); HttpResponseHeadersHandler headersModifier = new HttpResponseHeadersHandler(httpHeaders); ctx.addHandlerFirst(headersModifier);}ctx.addHandlerLast(new HttpContentDecompressor());
WebClient仅在特定的用例中使用此的实例,在这种情况下,我确定响应是压缩的。
编写很容易:Spring有一个
ResourceEnprer,因此
InputStream可以简单地转换为
InputStreamResource,瞧!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)