Use Case: Internal API Calls with OpenAPI Clients

About

In a microservices-based architecture, services often need to communicate with each other over HTTP. One common and robust way to do this is by generating strongly-typed clients based on shared OpenAPI specifications (also known as Swagger specs). This approach ensures that all services strictly adhere to the same contract, reduces manual boilerplate code, and makes changes more manageable through versioned specs.

This use case demonstrates a practical implementation where the payment-service consumes APIs from account-service using OpenAPI-generated clients.

Project Structure

Specs

  • account-api-spec Contains the OpenAPI definition (account.yaml) for retrieving account details.

  • payment-api-spec Contains payment.yaml defining how to retrieve payment details.

Service

  • account-service Implements the account API (as per spec) using Spring Boot.

  • payment-service Depends on both API specs and uses OpenAPI-generated clients to call account-service.

1. account-api-spec

Structure

account-api-spec/
├── assembly/api.xml
├── pom.xml
└── src/main/resources/openapi/account.yaml

api.xml

account.yaml

pom.xml

Build the package

2. payment-api-spec

Structure

api.xml

payment.yaml

pom.xml

Build the package

3. account-service

Structure

.openapi-generator-ignore

pom.xml

AccountController.java

AccountServiceApplication.java

application.yaml

Build the package

4. payment-service

Structure

.openapi-generator-ignore

pom.xml

AccountServiceRestClientConfig.java

PaymentController.java

PaymentService.java

PaymentServiceApplication.java

application.yaml

Build the package

Verification

Start both the services using mvn spring-boot:run

Test the API with Postman

Reference

Last updated