Skip to content

Instantly share code, notes, and snippets.

@tillmannheigel
Last active June 26, 2018 15:33
Show Gist options
  • Save tillmannheigel/abdef1bd03908689e10bcb22c94059c6 to your computer and use it in GitHub Desktop.
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.
@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