Spring Boot OpenAPI generator example

Updated: 2023-12-28

Update: The code has been updated for Spring Boot 3.x and Angular 17

In this example we show how to integrate OpenApi 3 (Swagger) in your Spring Boot application to generate your @RestController using a specification API.

Initially, we will generate only the backend code, in a second example we will generate the TypeScript frontend for Angular.

This tutorial is based on our example of Spring Boot and Angular architecture.

OpenAPI for Spring Boot 2 and Spring Boot 3

The branch for the Spring Boot 3.x version is available here: https://github.com/marco76/java-angular-basic/tree/external-api-open-api-spring-boot-3

The branch (tag) for the Spring Boot 2.x version is available here: https://github.com/marco76/java-angular-basic/tree/v0.0.1-Angular12_Spring2_OpenAPI

At the end of the post you have a list of changes required to migrate OpenAPI from Spring Boot 2 to Spring Boot 3.

Test the application

You can run the project with :
mvn clean package and java -jar ./backend/backend-0.1.1-SNAPSHOT.jar.

localhost:8080 should show the standard angular application, localhost:8080/swagger-ui/index.html should show the OpenAPI definition.

Architecture of the project

In our existing project we add a new module open-api

openapi folder structure

The backend project will reference the API generated by this project:

<!-- pom.xml (backend) --> 

The main pom needs to reference the new module too.

<!-- parent pom.xml --> 

The OpenApi Spring Boot module

This module is pretty small, it contains only the specifications of the API.

In our example we will generate the code directly in this module. Some developers prefer to include only the specification and generate the code directly in the consumer module.

swagger configuration files in folder

In the api pom.xml we need the following dependencies


You should check the most appropriate version for your project.

The work is done by the openapi generator plugin.

  <version>7.2.0</version> <!-- important for Spring Boot 3.x --> 
      <!-- path to the specification --> 
      <!--https://openapi-generator.tech/docs/generators/spring --> 
	   <!-- ... lot of parameters and configuration omitted here, look in the original file ... --> 
         <!-- configuration --> 
           <useJakartaEe>true</useJakartaEe> <!-- important for Spring Boot 3.x --> 
           <useSpringBoot3>true</useSpringBoot3> <!-- important for Spring Boot 3.x --> 

There are a lot of options configured, what is more important for us at this moment are the following lines:

<!-- path to the specification --> 
<!--https://openapi-generator.tech/docs/generators/spring --> 

<inputSpec> defines where is the specification file in our folder structure.

<generatorName> and <library> use a generator created specifically for Spring Boot that will allow us to generate directly @RestControllers from the specifications.

Specifications OpenApi 3

This is the content of our specifications file:

openapi: 3.0.3 # version of the specification 
  version: '1' 
  title: marco.dev Angular Spring Boot Example Api 
  - url: http://localhost:8080 
      summary: return a simple generic greeting 
      operationId: getGreeting 
          description: General greeting 
                $ref: '#/components/schemas/Greeting' 
      - in: path 
        name: name 
          type: string 
        required: true 
        description: "Name" 
        example: "Marco" 
      description: return a greeting with name 
      operationId: getPersonalGreeting 
          description: Personal greeting 
                $ref: '#/components/schemas/Greeting' 
      type: object 
          type: string 
          example: 'Hello from Spring' 
          default: 'Hello visitor' 

You can create your own and or test it on https://editor.swagger.io/.

We have two simple GET Responses and we have a Schema (represented in Java by a class) that will contain the message to send to the client.

If you open the browser at localhost:8080/swagger-ui/index.html you should see the OpenAPI definition.

swagger api UI

When we build the project with maven, the Java classes are generated.

swagger api UI

Our project includes an interface with the GET methods and a model with the class that will define the format of our answer. The ApiUtil is an helper class that contains some features not generated to h

Here is an example of the generated GET for /hello-open :

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen") 
@Api(value = "hello-open", description = "the hello-open API") 
public interface HelloOpenApi { 
    default Optional<NativeWebRequest> getRequest() { 
        return Optional.empty(); 
     * GET /hello-open : return a simple generic greeting 
     * @return General greeting (status code 200) 
    @ApiOperation(value = "return a simple generic greeting", nickname = "getGreeting", notes = "", response = Greeting.class, tags={  }) 
    @ApiResponses(value = {  
        @ApiResponse(code = 200, message = "General greeting", response = Greeting.class) }) 
        value = "/hello-open", 
        produces = { "application/json" } 
    default ResponseEntity<Greeting> getGreeting() { 
        getRequest().ifPresent(request -> { 
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) { 
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { 
                    String exampleString = "{ \"message\" : \"Hello from Spring\" }"; 
                    ApiUtil.setExampleResponse(request, "application/json", exampleString); 
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); 

There is a lot of stuff inside this class. What is relevant for us is that an interface containing some REST responses are generated and ready to be used.

On the base of the Greeting schema we have Greeting.java . Here we show only partially the content of the code. Interesting for us are the dependencies (Jackson, Hibernate, Swagger) used by the generator.

import java.util.Objects; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.annotation.JsonCreator; 
import io.swagger.annotations.ApiModel; 
import io.swagger.annotations.ApiModelProperty; 
import org.openapitools.jackson.nullable.JsonNullable; 
import java.io.Serializable; 
import javax.validation.Valid; 
import javax.validation.constraints.*; 
import org.hibernate.validator.constraints.*; 
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen") 
public class Greeting  implements Serializable { 
  private static final long serialVersionUID = 1L; 
  private String message = "Hello visitor"; 
  public Greeting message(String message) { 
    this.message = message; 
    return this; 
  @ApiModelProperty(example = "Hello from Spring", value = "") 
  public String getMessage() { 
    return message; 
  public void setMessage(String message) { 
    this.message = message; 

The code is complete overriding some standard methods:

java greeting result

The implementation in Spring Boot

In our main project (backend) we can implement the interfaces created, be sure that the package with the API is imported in your project and updated.

import dev.marco.example.api.controller.HelloOpenApi; 
import dev.marco.example.api.model.Greeting; 
import org.springframework.http.ResponseEntity; 
import org.springframework.web.bind.annotation.RestController; 
public class HelloControllerApiImpl implements HelloOpenApi { 
  public ResponseEntity<Greeting> getGreeting() { 
    Greeting greeting = new Greeting(); 
    greeting.message("Hello from Spring Boot"); 
    return ResponseEntity.ok(greeting); 
  public ResponseEntity<Greeting> getPersonalGreeting(String name) { 
      Greeting greeting = new Greeting(); 
      greeting.message("Hello " + name + ", enjoy Spring Boot"); 
      return ResponseEntity.ok(greeting); 

Here the complete implementation of our 2 GET methods. OpenApi (Swagger) manages the boilerplate code and our task is limited to the implementation.


Here you can find an image of the result when we call our method (getPersonalGreeting) from the browser

rest json answer in browser

OpenAPI migration from Spring Boot 2.x to Spring Boot 3.x

There are relevant changes between the 2 versions, here a summary.

SpringFox replaced with SpringDoc OpenAPI

We removed SpringFox for the removal of the specifications to use springdoc-openapi-starter-webmvc-ui in the main project.

You can find the migration documentation here: https://springdoc.org/migrating-from-springfox.html

Libraries changes

We replaced Hibernate validator to use a Spring dependency:

From org.hibernate:hibernate-validator:7.0.1.Final to org.springframework.boot:spring-boot-starter-validation.

We added a dependency to org.springframework.boot:spring-boot-starter-web (we removed it from the root pom).

We added a dependency io.swagger.core.v3:swagger-annotations:2.2.15.

package javax.validation does not exist: javax.annotation and javax.validation errors

There are multiple issues open in the OpenAPI project with this subject. A simple migration from Spring Boot 2.x to Spring Boot 3.x changes the dependencies to some javax libraries that have been renamed to jakarta.

The exception is :

You can find some debates around this issue here (still open at the end of 2023):

GitHub Issue 14143: Support SpringBoot 3.0 GA
GitHub Issue 141696: RestTemplate support for Spring boot 3

To solve the issue whe can tell the generator (goal generate in maven) that it has to use JakartaEE and Spring Boot 3:


To use these properties the openapi-generator has to be updated to a recent version (>= 6.3.0)

You could be interested in

logo of the category: spring-big.svg Spring Boot: REST controller Test example

How to test the @RestController with Spring Boot

logo of the category: spring-big.svg How to deploy a Java and Angular webapp in one JAR/WAR - Video tutorial

How to configure a fullstack Angular and Spring Boot application
Fullstack Angular / Java application quick start guide.
WebApp built by Marco using SpringBoot 3.2.4 and Java 21. Hosted in Switzerland (GE8).