ServerHttpResponseDecorator not work in GatewayFilter
See original GitHub issueI want to read response in GatewayFilter, I read it with ServerHttpResponseDecorator, but it not work. even can not debug into this code.
Here is my code:
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
ServerHttpResponseDecorator responseDecorator = new ServerHttpResponseDecorator(response) {
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
if (body instanceof Flux) {
return super.writeWith(Flux.from(body).buffer().map(buffers -> {
Charset charset = response.getHeaders().getContentType().getCharset();
charset = charset == null ? StandardCharsets.UTF_8 : charset;
HttpStatus status = response.getStatusCode();
StringBuffer responseBody = new StringBuffer();
for (DataBuffer buffer : buffers) {
responseBody.append(DataBufferParseUtils.toString(buffer, charset));
}
log.info("Response: {}", responseBody);
return DataBufferParseUtils.fromString(responseBody.toString(), charset);
}));
}
return super.writeWith(body);
}
};
return chain.filter(exchange.mutate().request(request).response(responseDecorator).build());
};
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:8
Top Results From Across the Web
Spring Cloud Gateway: Modified Response Body is Truncated
I resolved this issue using buffer() method: public class ModifyBodyGatewayFilterImpl implements GatewayFilter { @Override public Mono<Void> ...
Read more >spring-cloud/spring-cloud - Gitter
Hi there. My Spring Boot apps 1.5.x are using a Spring Cloud Config server to get their cfg. The latter used to be...
Read more >How to modify body of the spring boot reactive oauth2 ...
Note: i implemented GatewayFilter because i have this logic in gateway service level ... log the response body ServerHttpResponseDecorator decoratedResponse ...
Read more >Spring Cloud Gateway — Encryption/Decryption of Request ...
This post provides a working example of using Spring Cloud Gateway to decrypt an incoming request from the client, send the decrypted ...
Read more >获取SpringCloud gateway 响应的response的值,可以查看
Publisher; import org.springframework.cloud.gateway.filter. ... if body is not a flux. never got there. return super.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is working here
您可能会从中获取一些提示ModifyResponseBodyGatewayFilterFactory并将其应用于全局过滤器。这里的关键是您需要读取主体日志并重置交换中的响应,以便另一个过滤器可以再次读取主体(因为主体只能读取一次)。
您需要确保过滤器在 之前运行NettyWriteResponseFilter,您应该能够通过将顺序设置为 来做到这一点-2
code: /** * 神坑!!! * 要比NettyWriteResponseFilter更优先执行,如果已经被NettyWriteResponseFilter 输出了,则无法读取修改response * @return */ @Override public int getOrder() { return -4; }