Last active
June 26, 2018 15:33
-
-
Save tillmannheigel/abdef1bd03908689e10bcb22c94059c6 to your computer and use it in GitHub Desktop.
Minimal application.java file with static and dynamic header manipulation for Spring Cloud Gateway.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SpringBootApplication | |
public class Application { | |
@Bean | |
public RouteLocator staticRoutes(RouteLocatorBuilder builder) { | |
return builder | |
.routes() | |
.route(defaultRoute()) | |
.build(); | |
} | |
private Function<PredicateSpec, Route.Builder> defaultRoute() { | |
return r -> r | |
.path("/**") | |
.filters(defaultFilters()) | |
.uri("lb://application:8080"); | |
} | |
private Function<GatewayFilterSpec, UriSpec> defaultFilters() { | |
return f -> f | |
.preserveHostHeader() | |
.filter(this::addCustomHeader) | |
.addResponseHeader("x-static-router-header", random() + ""); | |
} | |
private Mono<Void> addCustomHeader(ServerWebExchange exchange, GatewayFilterChain chain) { | |
exchange.getResponse().getHeaders().add("x-dynamic-router-header", random() + ""); | |
return chain.filter(exchange); | |
} | |
private double random(){ | |
return Math.random(); | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(Application.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment