如何在Spring Gateway中获取响应体



我正在使用spring云网关过滤器,需要获取响应主体来记录它。我知道这是有问题的,因为spring-gateway是建立在spring-reactor上的,但尽管如此,我仍在寻找任何方法来做到这一点。

具有全局过滤器,代码:


import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyResponseBodyGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.rewrite.RewriteFunction;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
public class BodyRewrite implements RewriteFunction<byte[], byte[]> {
@Override
public Publisher<byte[]> apply(ServerWebExchange exchange, byte[] body) {
System.out.println("-------------------------");
System.out.println("       APPLY METHOD");
System.out.println("-------------------------");
String originalBody = body==null?"":new String(body);
if (!ServerWebExchangeUtils.isAlreadyRouted(exchange)) {
return Mono.just(originalBody.getBytes());
} else {
System.out.println("RESPONSE: " + originalBody);
}
return new Publisher<byte[]>() {
@Override
public void subscribe(Subscriber<? super byte[]> subscriber) {

}
};
}
}
@Component
class ModifyResponseBodyFilter implements GlobalFilter, Ordered {
@Autowired
private ModifyResponseBodyGatewayFilterFactory modifyResponseBodyGatewayFilterFactory;
@Autowired
private BodyRewrite bodyRewrite;
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
System.out.println("---------------------------");
System.out.println("       GLOBAL FILTER");
System.out.println("---------------------------");
GatewayFilter delegate=modifyResponseBodyGatewayFilterFactory.apply(new ModifyResponseBodyGatewayFilterFactory.Config()
.setRewriteFunction(byte[].class, byte[].class, bodyRewrite));
return delegate.filter(exchange, chain);
}
@Override
public int getOrder() {
return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER-1;
}

在控制台中,我只连续得到大约30次输出,而没有短语"的输出;APPLY METHOD";。

---------------------------
GLOBAL FILTER
---------------------------

我是一名新手,精通英语和编程。这里有一种方式,但可能并不优雅:使用ModifyResponseBodyGatewayFilterFactory创建一个modifyResponse BodyFilter,并实现RewriteFunction。

public class BodyRewrite implements RewriteFunction<byte[], byte[]> {
@Override
public Publisher<byte[]> apply(ServerWebExchange exchange, byte[] body) {
String originalBody = body==null?"":new String(body);
if (!ServerWebExchangeUtils.isAlreadyRouted(exchange)) {
return Mono.just(originalBody.getBytes());
} else {
// its the reponse body when already routed
}
}
}
public class ModifyResponseBodyFilter implements GlobalFilter, Ordered {
@Autowired
private ModifyResponseBodyGatewayFilterFactory modifyResponseBodyGatewayFilterFactory;
@Autowired
private BodyRewrite bodyRewrite;
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
GatewayFilter delegate=modifyResponseBodyGatewayFilterFactory.apply(new ModifyResponseBodyGatewayFilterFactory.Config()
.setRewriteFunction(byte[].class, byte[].class, bodyRewrite));
return delegate.filter(exchange, chain);
}
@Override
public int getOrder() {
return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER-1;
}
}

相关内容

  • 没有找到相关文章

最新更新