0

I need to read the request body before it gets consumed in biz logic.

So I'm planning to do below in my web filter:

  1. transform Flux of DataBuffer into a byte[]
  2. use the byte[] as I wish
  3. attache a new Flux of DataBuffer which built on byte[] in step1 back to ServerWebExchange (with ServerHttpRequestDecorator)

I've checked thread like: How to log request body in spring Webflux Java

But it seems to "get the request body when it gets consumed", while my case is to "get the request boy before it gets consumed".

That's why I need the bi-direction transformation between byte[] and Flux(DataBuffer).

I guess org.springframework.core.io.buffer.DataBufferUtils might be helpful, could anyone share some concrete code samples?

Thanks in advance!

1 Answers1

1
   DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
    
   byte[] bytes = ....
   DataBuffer buffer = bufferFactory.allocateBuffer(bytes.length);
   buffer.write(bytes);
   return Flux.just(buffer);
Dylan
  • 2,161
  • 2
  • 27
  • 51