DEV Community

Amit Misra
Amit Misra

Posted on

2 2

Converting path params to query params in Spring Cloud Gateway

Problem Statement

How can I convert path-params on the incoming request to query params for one of your APIs.

TL;DR

.filters(fs -> fs.rewritePath("/(?<country>.\\S+)/(?<locale>.\\S+)/v(?<version>\\d+)/my-endpoint(?<queryParams>.*)",
                        "/api/v2/my-endpoint?locale=${locale}&country=${country}&"))
Enter fullscreen mode Exit fullscreen mode

Solution

API evolution is part of Microservices based development. One of our APIs used to have locale and country as path params. As we were writing a new version of the same API, we decided to have those path-params passed on as query-params instead.

In order to support migration from old to the newer version we decided to add gateway between the clients and two versions of the API.

The challenge now becomes that the client will keep sending the locale and country information like before but we need to somehow convert those params before calling the newer version.

Surprisingly, we were able to do that with relative ease. All you need is the following FilterSpec being added to your route -

.filters(fs -> fs.rewritePath("/(?<country>.\\S+)/(?<locale>.\\S+)/v(?<version>\\d+)/my-endpoint(?<queryParams>.*)",
                        "/api/v2/my-endpoint?locale=${locale}&country=${country}&"))
Enter fullscreen mode Exit fullscreen mode

With that in place the our gateway is now able to route the request to both the versions of our API without any impact on the client. Here are some logs collected for the gateway application -

Incoming request http://localhost:8081/us/en/v1/my-endpoint?channel=WEB is routed to id: my-api-v2-/us/en/v1/my-endpoint, uri:http://localhost:8080/api/v2/my-endpoint?locale=en&country=us&?channel=WEB
Enter fullscreen mode Exit fullscreen mode

The above log statement was generated by using the code available on this thread on StackOverFlow

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay