Use Case: Internal API Calls with OpenAPI Clients

About

In a typical microservices architecture, services frequently need to communicate with each other to perform operations or retrieve data. This use case demonstrates how OpenAPI-generated clients can be used to make internal service-to-service calls in a Spring Boot-based system, ensuring:

  • Strong typing and IDE support

  • Reduced boilerplate

  • Consistent request and response handling

  • Alignment with the contract defined in OpenAPI specs

This setup uses two services:

  • account-service: Exposes an endpoint to retrieve account details.

  • payment-service: Calls the account-service using a client generated from account-api-spec.

Both services are aligned on their contracts using shared OpenAPI specifications and generate client code during build time using the OpenAPI Generator with the spring-webclient library.

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

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