Course – Black Friday 2025 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Partner – Orkes – NPI EA (cat=Java)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

Regression testing is an important step in the release process, to ensure that new code doesn't break the existing functionality. As the codebase evolves, we want to run these tests frequently to help catch any issues early on.

The best way to ensure these tests run frequently on an automated basis is, of course, to include them in the CI/CD pipeline. This way, the regression tests will execute automatically whenever we commit code to the repository.

In this tutorial, we'll see how to create regression tests using Selenium, and then include them in our pipeline using GitHub Actions:, to be run on the LambdaTest cloud grid:

>> How to Run Selenium Regression Tests With GitHub Actions

Course – Black Friday 2025 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

1. Introduction

It’s a common task to implement services that demand some form of network communication. In such cases, we typically need to write server and client code to enable that communication.

In this article, we’ll learn how to automatically generate server and client code using the OpenAPI Specification, focusing on the client-generated code.

2. Defining a Demo API in YAML

The OpenAPI Specification utilizes a YAML file definition to describe an API’s structure. Both server and client applications can seamlessly import that definition to generate server and client code.

To illustrate client-code generation, let’s define a demo weather API using a weatherapi.yaml file:

openapi: 3.0.3
info:
  title: Current Weather API
  description: |
    Get real-time weather information for cities worldwide.
  version: 1.0.0

paths:
  /weather:
    get:
      summary: Get current weather data
      description: Retrieve current weather information for a specified city
      operationId: getCurrentWeather
      parameters:
        - name: city
          in: query
          required: true
          schema:
            type: string
        - name: units
          in: query
          required: false
          schema:
            type: string
            enum: [ celsius, fahrenheit ]
            default: celsius
      responses:
        '200':
          description: Successful weather data retrieval
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/WeatherResponse'
        '404':
          description: City not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

components:
  schemas:
    WeatherResponse:
      type: object
      required:
        - location
      properties:
        current:
          type: object
          required:
            - temperature
            - units
          properties:
            temperature:
              type: number
              format: double
            units:
              type: string
              enum: [ celsius, fahrenheit ]
            timestamp:
              type: string
              format: date-time

    ErrorResponse:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: string
        message:
          type: string

The API has a single REST endpoint that gets the current weather and returns a success response or an error.

3. Configuring the Project

We’ll need a few dependencies and a Maven plugin configuration to generate Java code from our YAML file.

3.1. Adding the Maven Dependencies

To compile the generated code correctly, we’ll need the spring-boot-starter-web and spring-starter-validation dependencies to get a few web classes and annotations from there:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

3.2. Configuring the OpenAPI Maven Plugin

We should also set the openapi-generator-maven-plugin execution in the OpenAPI dependency to generate client code:

<plugins>
    <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>${openapi-generator.version}</version>
        <executions>
            <execution>
                <id>generate-weather-api</id>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <inputSpec>${project.basedir}/src/main/resources/api/weatherapi.yaml</inputSpec>
                    <generatorName>spring</generatorName>
                    <configOptions>
                        <openApiNullable>false</openApiNullable>
                        <useJakartaEe>false</useJakartaEe>
                        <documentationProvider>none</documentationProvider>
                        <annotationLibrary>none</annotationLibrary>
                        <apiPackage>com.baeldung.tutorials.openapi.generatehttpclients.api</apiPackage>
                        <modelPackage>com.baeldung.tutorials.openapi.generatehttpclients.api.model</modelPackage>
                    </configOptions>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
</plugins>

In the snippet above, we defined a Maven execution to generate the code from our YAML file from the configuration set:

  • inputSpec: the location of our YAML file
  • generatorName: a generator from the ones available. We’re typically interested in either the java or spring generators
  • openApiNullable: whether the code should use Jackson Nullable library or not
  • useJakartaEe: whether the code should use the jakarta namespace instead of javax
  • documentationProvider: the source of documentation for the API. It’s either none or source. The latter means that it’ll document as described in the YAML file.
  • annotationLibrary: the annotation library to use in documentation if documentationProvider is set to non-none
  • apiPackage: the target package for API-related classes
  • modelPackage: the target package for model classes

Another important configOption that was not listed there is library. This is the library that the generated code uses for implementation.

We’re using the default library, a simple test implementation using Spring’s vanilla web and http packages. For that article, we’re only interested in getting a ready-to-use REST client based on our OpenAPI Specification file, so that basic test implementation works for illustrative purposes.

OpenAPI provides different options for library by setting the generatorName to spring, such as implementations using auto-configured Spring Cloud OpenFeign beans.

Additionally, setting generatorName to java provides a broader set of options for the library we want to generate code for, including RestTemplateOpenFeignand RestAssured.

4. Generating API Clients

After setting up basic OpenAPI configuration, we can generate code using the Maven plugin by running:

mvn compile

After that, we’ll be able to see the generated classes under the path in the target folder:

Folder structure showing generated classes using OpenAPI generator plugin for Maven

We can see two important files there: the WeatherApi interface and the WeatherApiController. Let’s glance at how OpenAPI generator generated WeatherApi:

public interface WeatherApi {
    @RequestMapping(
        method = RequestMethod.GET,
        value = "/weather",
        produces = { "application/json" }
    )
    
    default ResponseEntity<WeatherResponse> getCurrentWeather(
        @NotNull  @Valid @RequestParam(value = "city", required = true) String city,
         @Valid @RequestParam(value = "units", required = false, defaultValue = "celsius") String units
    ) {
        // a generated test implementation
    }
}

After removing some clutter from the generated code, we get the getCurrentWeather() method, which defines the endpoint at /weather using the @RequestMapping annotation. That method serializes, validates, and provides defaults for the input as we defined in our YAML specification, using @Valid, @NotNull, and @RequestParam annotations.

Then, if we look at the WeatherApiController:

@Controller
@RequestMapping("${openapi.currentWeather.base-path:}")
public class WeatherApiController implements WeatherApi {
    // a generated test implementation
}

OpenAPI also generated a Spring auto-configured implementation of WeatherApiController, which uses an environment variable in @RequestMapping, so clients that import that bundle into their projects can set the API base path via configuration. For instance, at the client side, we could import an exported module that has the api generated package and just set a base-path resource and inject WeatherApi automatically:

@Service
public class GetWeatherService {
    private final WeatherApi weatherApi;

    public GetWeatherService(WeatherApi weatherApi) {
        this.weatherApi = weatherApi;
    }

    public WeatherResponse getCurrentWeather(String city, String units) {
        var response = weatherApi.getCurrentWeather(city, units);

        if (response.getStatusCodeValue() < 399) {
            return response.getBody();
        }

        throw new RuntimeException("Failed to get current weather for " + city);
    }
}

And at the client’s application.yaml:

openapi:
  currentWeather:
    base-path: https://localhost:8080

5. Advantages of OpenAPI

The OpenAPI generator is handy when a service exposes an endpoint and has clients that depend on it. In that case, we can maintain two modules, one for clients and another for the server, both of which read the API contract from the same OpenAPI YAML specification.

Hence, both modules would be up-to-date with the latest API contract, thereby minimizing the risk of breaking the contract between client and server applications. That requires a more granular management of the client module versioning, and requires that clients keep the library up-to-date on their side.

Notably, using code generators provides us with the advantage of skipping part of the manual code implementation. Thus, we can reduce the effort required to create communication between the client and server, as the code on both sides can be auto-generated.

For instance, in a situation where the same developer needs to implement both the server and client code, it could be productive to reduce those two efforts to just creating the OpenAPI YAML specification file and let the generator do its job.

6. Drawbacks of OpenAPI

As with any code generator, we have the drawback of not having total control over how the generated code looks. For more customized and complex client code needs, it can be painful to use generators, as we lack control over what’s being generated.

For instance, OpenAPI currently generates mutable model classes for API responses, which it could instead leverage Java Records for simplicity and safety.

Additionally, with generated code, we also lack control over debugging, troubleshooting, and fixing code, as we can’t modify it.

Finally, it introduces another dependency with a set of transitive dependencies, which requires more effort to update and avoid incompatibility and security issues.

7. Conclusion

In this article, we’ve seen how to auto-generate client code using OpenAPI Specification and the OpenAPI Maven plugin. We’ve addressed the typical case of generating simple REST clients using the spring-web dependency and the API described in an OpenAPI Specification YAML file.

We’ve also examined the advantages and disadvantages of using such a library to generate code for our use.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Course – Black Friday 2025 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2025 – NPI (All)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

Course – LS – NPI (cat=HTTP Client-Side)
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

eBook Jackson – NPI EA – 3 (cat = Jackson)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments