'Spring Cloud Gateway - How To Modify Response Body In Post Filter
So far, I've not seen any solutions that is working for me. I've tried this and this.
Here is my custom filter:
@Component
public class TestFilter implements GlobalFilter, Ordered {
@Autowired
private ModifyResponseBodyGatewayFilterFactory modifyFilter;
@Autowired
private rewriteBody bodyRewrite;
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return chain.filter(exchange).map(ex -> {
GatewayFilter delegate = modifyFilter.apply(new ModifyResponseBodyGatewayFilterFactory.Config()
.setRewriteFunction(byte[].class, byte[].class, bodyRewrite));
delegate.filter(exchange, chain);
return ex;
});
}
@Override
public int getOrder() {
return -1;
}
@Component
public class rewriteBody implements RewriteFunction<byte[], byte[]> {
@Override
public Publisher<byte[]> apply(ServerWebExchange exchange, byte[] body) {
byte[] newBody = "New response".getBytes();
return Mono.just(newBody);
}
}
}
The ModifyResponseBodyGatewayFilterFactory
works if I implement it in a pre-filter, but how do I modify it in the post-filter.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|