Add post filter example

This commit is contained in:
Spencer Gibb
2016-11-17 11:47:26 -07:00
parent b3a5dab1c8
commit ed081e0d20
2 changed files with 28 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ package org.springframework.cloud.gateway;
import java.util.Optional;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
@@ -16,6 +17,7 @@ import reactor.core.publisher.Flux;
* @author Spencer Gibb
*/
@RestController
@SuppressWarnings("unused")
public class GatewayController {
private final WebClient webClient;
@@ -38,7 +40,8 @@ public class GatewayController {
ServerHttpResponse response = exchange.getResponse();
response.getHeaders().putAll(clientResponse.headers().asHttpHeaders());
response.setStatusCode(clientResponse.statusCode());
return response.writeWith(clientResponse.body((inputMessage, context) -> inputMessage.getBody()));
Flux<DataBuffer> body = clientResponse.body((inputMessage, context) -> inputMessage.getBody());
return response.writeWith(body);
});
}
}

View File

@@ -1,16 +1,23 @@
package org.springframework.cloud.gateway;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import reactor.core.publisher.Mono;
@SpringBootApplication
public class SpringCloudGatewayApplication {
private static final Log log = LogFactory.getLog(SpringCloudGatewayApplication.class);
@Bean
public WebClient webClient() {
return WebClient.builder(new ReactorClientHttpConnector()).build();
@@ -21,6 +28,7 @@ public class SpringCloudGatewayApplication {
@Order(500)
public WebFilter findRouteFilter() {
return (exchange, chain) -> {
log.info("findRoutFilter start");
exchange.getAttributes().put("requestUrl", "http://httpbin.org/get");
return chain.filter(exchange);
};
@@ -30,11 +38,27 @@ public class SpringCloudGatewayApplication {
@Order(501)
public WebFilter modifyResponseFilter() {
return (exchange, chain) -> {
log.info("modifyResponseFilter start");
exchange.getResponse().getHeaders().add("X-My-Custom", "MyCustomValue");
return chain.filter(exchange);
};
}
@Bean
@Order(502)
public WebFilter postFilter() {
return (exchange, chain) -> {
log.info("postFilter start");
return chain.filter(exchange).then(postFilterWork(exchange));
};
}
private static Mono<Void> postFilterWork(ServerWebExchange exchange) {
log.info("postFilterWork");
exchange.getResponse().getHeaders().add("X-Post-Header", "AddedAfterRoute");
return Mono.empty();
}
public static void main(String[] args) {
SpringApplication.run(SpringCloudGatewayApplication.class, args);
}