Spring Cloud Gateway请求体到请求头的转换,通过自定义过滤器实现。

huangapple go评论45阅读模式
英文:

Spring cloud gateway request body to request header use by custom filter

问题

以下是您提供的代码的翻译部分:

@Component
@Slf4j
public class GetFormGatewayFilterFactory extends AbstractGatewayFilterFactory<GetFormGatewayFilterFactory.Config> {
    public GetFormGatewayFilterFactory() {
        super(Config.class);
    }
    @Override
    public GatewayFilter apply(Config config) {
        //自定义预过滤器
        return (exchange, chain) -> {

            return DataBufferUtils.join(exchange.getRequest().getBody())
                    .flatMap(dataBuffer -> {
                        //读取请求体数据
                        byte[] bytes = new byte[dataBuffer.readableByteCount()];
                        dataBuffer.read(bytes);
                        DataBufferUtils.release(dataBuffer);
                        String body = new String(bytes, StandardCharsets.UTF_8);

                        //将读取的请求体数据添加到请求头中

                        return chain.filter(exchange.mutate().request(exchange.getRequest().mutate().header("headerName", body).build()).build());
                    });
        };
    }

    public static class Config {
        //放置配置属性
    }
}

请注意,翻译是基于您提供的代码进行的,不包括注释部分。如果您需要其他翻译或有任何其他问题,请随时提出。

英文:

I am new to webflux.
I am having difficulty controlling data using webflux.

Body data is missing in the controller when writing as below. Is there a solution?
Data sent as body value is set as header
Reading the body data seems to be successful.
However, data is not added to the header value, and the body value information is also lost.

@Component
@Slf4j
public class GetFormGatewayFilterFactory extends AbstractGatewayFilterFactory<GetFormGatewayFilterFactory.Config> {
    public GetFormGatewayFilterFactory() {
        super(Config.class);
    }
    @Override
    public GatewayFilter apply(Config config) {
        //Custom Pre Filter
        return (exchange, chain) -> {

            return DataBufferUtils.join(exchange.getRequest().getBody())
                    .flatMap(dataBuffer -> {
                        //바디값 읽어오기
                        byte[] bytes = new byte[dataBuffer.readableByteCount()];
                        dataBuffer.read(bytes);
                        DataBufferUtils.release(dataBuffer);
                        String body = new String(bytes, StandardCharsets.UTF_8);

                        //읽어온 바디값을 header값에 추가

                        return chain.filter(exchange.mutate().request(exchange.getRequest().mutate().header("headerName", body).build()).build());
                    });
        };
    }



    public static class Config {
//Put the configuration properties
    }

}```

</details>


# 答案1
**得分**: 0

在Webflux中读取请求主体时要记住的一件事是,您将会消耗Flux&lt;DataBuffer&gt;。重要的是要注意,这个Flux只能被消耗一次,这意味着您应该将其缓存并将缓存传递给控制器。但在Webflux中缓存请求主体并不是一个好主意,因为这会抵消WebFlux的一些好处。有关更多信息,请参考以下链接:
https://stackoverflow.com/a/76250029/6933090

在这里,您可以找到有关如何缓存请求主体的一些额外信息:
https://www.baeldung.com/kotlin/spring-webflux-log-request-response-body

<details>
<summary>英文:</summary>

One thing to keep in mind is that when reading the request body in Webflux, you will be consuming the Flux&lt;DataBuffer&gt;. It&#39;s important to note that this Flux can only be consumed once, which means you should cache it and pass the cache along to the controller. But caching the request body in Webflux is not the best idea since it negates some benefits of WebFlux. For additional information, please refer to the following link: 
https://stackoverflow.com/a/76250029/6933090

Here you can find some additional information on how to cache the request body:
https://www.baeldung.com/kotlin/spring-webflux-log-request-response-body



</details>



huangapple
  • 本文由 发表于 2023年5月17日 13:49:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定