0
0
Spring Bootframework~30 mins

Generating client code from spec in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Generating client code from spec
📖 Scenario: You are building a Spring Boot application that needs to communicate with an external REST API. The API provides an OpenAPI specification (a contract describing its endpoints and data). To make your work easier, you want to generate client code automatically from this spec instead of writing HTTP calls manually.
🎯 Goal: Learn how to generate Spring Boot client code from an OpenAPI specification file. You will create the spec file, configure the code generator, generate the client code, and integrate it into your Spring Boot project.
📋 What You'll Learn
Create an OpenAPI spec file named petstore.yaml with a simple endpoint
Add a configuration property to specify the location of the spec file
Use the OpenAPI Generator Maven plugin to generate client code from the spec
Include the generated client code in your Spring Boot project
💡 Why This Matters
🌍 Real World
Generating client code from an API spec saves time and reduces errors when integrating with external services. It ensures your client matches the API contract exactly.
💼 Career
Many companies use OpenAPI specs and code generators to build reliable API clients quickly. Knowing how to set this up is a valuable skill for backend and full-stack developers.
Progress0 / 4 steps
1
Create the OpenAPI spec file
Create a file named petstore.yaml in the src/main/resources folder. Add the following minimal OpenAPI spec content exactly as shown:
openapi: 3.0.0
info:
  title: Petstore API
  version: 1.0.0
paths:
  /pets:
    get:
      summary: List all pets
      responses:
        '200':
          description: A list of pets
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
Spring Boot
Need a hint?

Make sure the file is named exactly petstore.yaml and placed inside src/main/resources.

2
Add OpenAPI spec location configuration
In your pom.xml file, add a property named openapi.spec.path with the value src/main/resources/petstore.yaml. This will tell the generator where to find the spec file.
Spring Boot
Need a hint?

Add the property inside the <properties> section of your pom.xml.

3
Configure OpenAPI Generator Maven plugin
In your pom.xml, add the OpenAPI Generator Maven plugin inside the <plugins> section. Configure it to use the property ${openapi.spec.path} as the input spec, generate a Spring client, and output to target/generated-sources/openapi. Use the artifactId openapi-generator-maven-plugin and version 6.6.0.
Spring Boot
Need a hint?

Place the plugin inside the <build><plugins> section of your pom.xml.

4
Include generated client code in Spring Boot project
In your pom.xml, add the generated sources folder target/generated-sources/openapi/src/main/java as a source directory so that Spring Boot can compile the generated client code. Add this inside the <build> section using <sourceDirectory> or <build><plugins><plugin><configuration><sourceDirectories> as appropriate.
Spring Boot
Need a hint?

Add the generated sources folder inside <sourceDirectories> in the <build> section of your pom.xml.