Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spring Cloud Gateway. How to make response locally without route to external service

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()
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading