Bullet-tooth
Bullet-tooth

Reputation: 814

Spring webflux: how to configure Controller and WebClient to work like proxy?

I need an endpoint that will work in proxy mode: forward requests to external REST API. Currently I implemented such class but it's very far away from being ideal.

import java.net.URI;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

@RestController
public class ProxyController2 {

  private static final int OFFSET = 4;
  private static final String SCHEME = "http";
  private static final String HOTS = "127.0.0.1";
  private static final String PORT = "9090";

  @RequestMapping("/proxy/public/**")
  public Mono<String> publicProxy(ServerHttpRequest request) {
    HttpMethod httpMethod = request.getMethod();

    if (bodyRequired(httpMethod)) {
      return WebClient.create()
          .method(httpMethod)
          .uri(composeTargetUri(request))
          .headers(headers -> headers.addAll(request.getHeaders()))
          .body(BodyInserters.fromDataBuffers(request.getBody()))
          .retrieve()
          .bodyToMono(String.class);
    } else {
      return WebClient.create()
          .method(httpMethod)
          .uri(composeTargetUri(request))
          .headers(headers -> headers.addAll(request.getHeaders()))
          .retrieve()
          .bodyToMono(String.class);
    }
  }

  private URI composeTargetUri(ServerHttpRequest request) {
    return UriComponentsBuilder.newInstance()
        .scheme(SCHEME)
        .host(HOTS)
        .port(PORT)
        .path(getTargetPath(request))
        .build()
        .toUri();
  }

  private String getTargetPath(ServerHttpRequest request) {
    return request.getPath().pathWithinApplication()
        .subPath(OFFSET)
        .value();
  }

  private boolean bodyRequired(HttpMethod httpMethod) {
    return httpMethod == HttpMethod.DELETE || httpMethod == HttpMethod.POST
        || httpMethod == HttpMethod.PUT;
  }
}

It has a few drawbacks:
* It always returns results as string.
* We lose response headers.
* We lose response status (It produces 500 with error message description).

Do you know the good way to create proxy controller in spring webflux application?

Upvotes: 4

Views: 6260

Answers (1)

Barath
Barath

Reputation: 5283

Spring Cloud Gateway

An API Gateway built on top of the Spring Ecosystem, including: Spring 5, Spring Boot 2 and Project Reactor. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.

Doc: Spring Cloud Gateway

Upvotes: 3

Related Questions