copy headers and add filters.
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
package org.springframework.cloud.gateway;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -20,26 +19,26 @@ import reactor.core.publisher.Flux;
|
||||
public class GatewayController {
|
||||
|
||||
private final WebClient webClient;
|
||||
private final DataBufferFactory bufferFactory;
|
||||
|
||||
public GatewayController() {
|
||||
webClient = WebClient.builder(new ReactorClientHttpConnector()).build();
|
||||
bufferFactory = new DefaultDataBufferFactory();
|
||||
public GatewayController(WebClient webClient) {
|
||||
this.webClient = webClient;
|
||||
}
|
||||
|
||||
//TODO: plugin to request mappings
|
||||
@GetMapping(path = "/")
|
||||
public Flux<Void> home(ServerWebExchange exchange) {
|
||||
ClientRequest<Void> request = ClientRequest
|
||||
.GET("http://httpbin.org/get")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
Optional<String> requestUrl = exchange.getAttribute("requestUrl");
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
ClientRequest<Void> clientRequest = ClientRequest
|
||||
.method(request.getMethod(), requestUrl.get())
|
||||
.headers(request.getHeaders())
|
||||
.build();
|
||||
|
||||
return this.webClient.exchange(request).flatMap(clientResponse -> {
|
||||
return this.webClient.exchange(clientRequest).flatMap(clientResponse -> {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
response.getHeaders().setContentType(clientResponse.headers().contentType().get());
|
||||
response.getHeaders().putAll(clientResponse.headers().asHttpHeaders());
|
||||
response.setStatusCode(clientResponse.statusCode());
|
||||
return response.writeWith(clientResponse.body((inputMessage, context) -> inputMessage.getBody()));
|
||||
// return response;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,39 @@ package org.springframework.cloud.gateway;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||
import org.springframework.web.client.reactive.WebClient;
|
||||
import org.springframework.web.server.WebFilter;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringCloudGatewayApplication {
|
||||
|
||||
@Bean
|
||||
public WebClient webClient() {
|
||||
return WebClient.builder(new ReactorClientHttpConnector()).build();
|
||||
}
|
||||
|
||||
// TODO: request only, how to filter response?
|
||||
@Bean
|
||||
@Order(500)
|
||||
public WebFilter findRouteFilter() {
|
||||
return (exchange, chain) -> {
|
||||
exchange.getAttributes().put("requestUrl", "http://httpbin.org/get");
|
||||
return chain.filter(exchange);
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(501)
|
||||
public WebFilter modifyResponseFilter() {
|
||||
return (exchange, chain) -> {
|
||||
exchange.getResponse().getHeaders().add("X-My-Custom", "MyCustomValue");
|
||||
return chain.filter(exchange);
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringCloudGatewayApplication.class, args);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user