1

I have Spring Boot microservice, and sending large payload using swagger. At the server I get only 15000 chars and reset 2000 chars are not read.

How can I use ReadBodyPredicateFactory to cache the body message text?

I am using springcloudgateway and added filters. In the filter in apply method I am trying to read the payload json using

  DefaultServerRequest serverRequest = new DefaultServerRequest(exchange);
  body = serverRequest.bodyToMono(String.class).toFuture().get();

Sometimes it hangs.

I tried with Flux and then i get only half message Flux body = request.getBody();

    body.subscribe(buffer -> {

        try {
            System.out.println("byte count:" + 
           buffer.readableByteCount());
            byte[] bytes = new byte[buffer.readableByteCount()];
            buffer.read(bytes);
            DataBufferUtils.release(buffer);
            String bodyString = new String(bytes, StandardCharsets.UTF_8);
            sb.append(bodyString);
        } catch (Exception e) {
            e.printStackTrace();
        }
0

1 Answer 1

6

Recently, I needed the similar thing in my application and I've found that it can be achieved by Spring Cloud Gateway built-in caching in ServerWebExchangeUtils

Before filters that use request content in some business cases, I created a filter that only forces content caching:

@Component
class CachingRequestBodyFilter extends AbstractGatewayFilterFactory<CachingRequestBodyFilter.Config> {

    public CachingRequestBodyFilter() {
        super(Config.class);
    }

    public GatewayFilter apply(final Config config) {
        return (exchange, chain) -> ServerWebExchangeUtils.cacheRequestBody(exchange,
            (serverHttpRequest) -> chain.filter(exchange.mutate().request(serverHttpRequest).build()));
    }

    public static class Config {
    }
}

In any of the subsequent filters, we can extract the content of the request body, as below:

// some ReadRequestBodyFilter filter

public GatewayFilter apply(final Config config) {
    return (exchange, chain) -> {
        final var cachedBody = new StringBuilder();
        final var cachedBodyAttribute = exchange.getAttribute(CACHED_REQUEST_BODY_ATTR);
        if (!(cachedBodyAttribute instanceof DataBuffer)) {
            // caching gone wrong error handling
        }
        final var dataBuffer = (DataBuffer) cachedBodyAttribute;
        cachedBody.append(StandardCharsets.UTF_8.decode(dataBuffer.asByteBuffer()).toString());
        final var bodyAsJson = cachedBody.toString();

        // some processing

        return chain.filter(exchange);
    };
}

Then the gateway configuration would look like this:

spring:
  cloud:
    gateway:
      routes:
        - [...]
          filters:
            - CachingRequestBodyFilter
            - ReadRequestBodyFilter
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.