API-First with Spring WebFlux and OpenAPI Generator
In this story, we’ll explore how to generate codes using the swagger-codegen from the defined OpenAPI file in a spring reactive application.

· What we need · Overview ∘ What is API-First? ∘ Why API-First? · OpenAPI Specification · Spring Webflux Application · Test the API ∘ Import OpenAPI definition in insomnia ∘ Test · Conclusion · References
Teams and organizations continue to embrace an API-first philosophy. — 2022 State of the API Report
In recent years, APIs have become the building blocks of software for companies and organizations. These companies use them to offer new services for their internal or external partners. Therefore, it is necessary to provide service contracts, with documentation that constitutes an agreement between the different stakeholders.
What we need
- Spring Boot 3
- Maven 3.8.+
- Java 17
- Postman or Insomnia
Overview
What is API-First?
API-first development is a development model in which applications are conceptualized and built by composing internal or external services delivered through APIs. This approach involves designing every API around a contract written in an API description language like OpenAPI.
Why API-First?
- Increasing developer productivity: Developers do not have to wait for updates to an API to be released before moving on to the next API. Teams can mock APIs and test API dependencies based on the established API definition.
- Reduces the risk of failure: API-First reduces the risk of failure by ensuring that APIs are reliable, consistent, and easy for developers to use.
- Reduces development cost: APIs specifications can be reused on many different projects. When a development team wants to build a new app, they don’t have to start from scratch which is time-consuming and costly. API-First design also allows most problems to be solved before any code is even written which helps prevent problems when it is time to integrate APIs with applications.
- Increases speed: API-First makes it possible to add new services and technologies to applications without having to re-architect the entire system. It allows businesses to optimize their speed to market by reusing existing software.
- Improve developer Experience: API-First ensures that developers have positive experiences using your APIs. Well-designed, well-documented, consistent APIs provide positive developer experiences because it’s easier to reuse code and onboard developers, and it reduces the learning curve.
OpenAPI Specification
The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for HTTP APIs, which allows both humans and computers to discover and understand the capabilities of a service without requiring access to source code, additional documentation, or inspection of network traffic.
The first thing to do is to define our API specification. In this story, we will create a library API, which will expose service contracts for CRUD operations.
An OpenAPI document that conforms to the OpenAPI Specification is itself a JSON object, which may be represented either in JSON or YAML format. For this story, we will use YAML format.
Our YAML specifications file:














