Skip to content

Instantly share code, notes, and snippets.

.route(r -> r.path("/addThenSubtract")
.filters(f -> f.filter(addAndSubtractCompositionFilter.apply(new Object())))
.uri(NOOP))
package no.embriq.api.gateway.filter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
package no.embriq.api.gateway.filter;
import com.fasterxml.jackson.databind.JsonNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatusCode;
@AdrianoJS
AdrianoJS / Routes.java
Created April 12, 2023 19:09
spring-cloud-gateway-example-stage-4-Routes
.route(r -> r.path("/ping")
.filters(f -> f.filter(pingFilter.apply(new Object()))) // pingFilter.apply parameter cannot be null even though it is ignored. Throws NPE
.uri("https://mandatory.url.that.is.ignored.due.to.filter.logic"))
package no.embriq.api.gateway.filter;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpStatusCode;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import static java.nio.charset.StandardCharsets.UTF_8;
@AdrianoJS
AdrianoJS / GlobalLoggingFilter.java
Created April 12, 2023 19:04
spring-cloud-gateway-example-stage-3-GlobalLoggingFilter
package no.embriq.api.gateway.filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@AdrianoJS
AdrianoJS / Routes.java
Created April 12, 2023 19:01
spring-cloud-gateway-example-stage-2-routes
package no.embriq.api.gateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
@AdrianoJS
AdrianoJS / route_description.csv
Last active April 12, 2023 18:51
spring-cloud-gateway-example-route-description
name required description
id no Human identifier for the route
uri yes The base URL for the receiving service. Does not need or use the path part of a url as this is retrieved from the request.
predicates yes Defines what requests trigger this route. In the above examples, routes are identified by path, but can also be selected based on time, cookies, headers etc.
filters no Optional modification of input and output
@AdrianoJS
AdrianoJS / application.yaml
Created April 12, 2023 18:47
spring-cloud-gateway-example-stage-1-application.yaml
server:
port: 8080
app:
dependencies:
foo:
baseUrl: http://localhost:8081
bar:
baseUrl: http://localhost:8082
@AdrianoJS
AdrianoJS / GatewayApplication.java
Created April 12, 2023 18:46
spring-cloud-gateway-example-gatewayApplication
package no.embriq.api.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "no.embriq.api.gateway")
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);