I need to provide mocked response for some endpoints with all configured filters, but couldn’t find any information about that possibility (maybe I can’t ask right question to google, who knows).
I need something like this (sorry for Kotlin, I’ll also be happy to see a Java solution)
return builder.routes {
route(id = "proxy", uri = uri) {
path(*PROXY_ROUTES)
filters {
this.filter(ProjectExtractor)
this.filter(participantFilter)
this.filter(ProxyPermissionsFilter)
prefixPath("/admin")
this.filter(adminHeaderEnricher)
}
}
route(id = "mock") {
path("mocker_route/**")
filters {
this.filter(ProjectExtractor)
this.filter(participantFilter)
this.filter(ProxyPermissionsFilter)
prefixPath("/admin")
this.filter(adminHeaderEnricher)
}
MyCustomExchangeHandler()
}
}
>Solution :
In Spring Cloud Gateway, you can create routes that handle requests and produce responses locally without forwarding them to external services. This is useful for scenarios where you want to handle specific requests within your gateway application itself. To achieve this, you can use a custom org.springframework.cloud.gateway.filter.GatewayFilter or leverage built-in filters provided by Spring Cloud Gateway.
return builder.routes()
.route("local-response-route", r -> r
.path("/local-response")
.filters(f -> f.filter(new LocalResponseFilter()))
.uri("no://op") // This is just a placeholder URI
)
.build();