Spring Top

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

Java Top

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


1. Overview

As the name suggests, the OpenAPI Generator generates code from an OpenAPI specification. It can create code for client libraries, server stubs, documentation, and configuration.

It supports various languages and frameworks. Notably, there's support for C++, C#, Java, PHP, Python, Ruby, Scala – almost all the widely used ones.

In this tutorial, we'll learn how to implement a Spring-based server stub using OpenAPI Generator via its maven plugin. Other ways of using the generator are through its CLI or online tools.

2. YAML File

To begin with, we'll need a YAML file specifying the API. We'll give it as input to our generator to produce a server stub.

Here's a snippet of our petstore.yml:

openapi: "3.0.0"
      summary: List all pets
      operationId: listPets
        - pets
        - name: limit
          in: query
      summary: Create a pet
      operationId: createPets
      summary: Info for a specific pet
      operationId: showPetById
      type: object
        - id
        - name
          type: integer
          format: int64
          type: string
          type: string
      type: object
        - code
        - message
          type: integer
          format: int32
          type: string

3. Maven Dependencies

3.1. Plugin for OpenAPI Generator

Next, let's add the Maven dependency for the generator plugin:


As we can see, we passed in the YAML file as inputSpec. After that, since we need a Spring-based server, we used the generatorName as spring.

Then apiPackage specifies the package name where the API will be generated into. Next, we have the modelPackage where the generator places the data models. With delegatePattern set to true, we're asking to create an interface that can be implemented as a customized @Service class.

Importantly, options for OpenAPI Generator are the same whether you're using the CLI, Maven/Gradle Plugins, or online generation options.

3.2. Spring Dependencies

As we'll be generating a Spring server, we also need its dependencies (Spring Boot Starter Web and Spring Data JPA) so that generated code compiles and runs as expected:


4. Code Generation

To generate the server stub, we simply need to run:

mvn clean install

As a result, we get:

Now let's take a look at the code, starting with the contents of apiPackage.

First, we get an API interface called PetsApi, that contains all the requests mappings as defined in the YAML specification. Here's the snippet:

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", 
  date = "2021-03-22T23:26:32.308871+05:30[Asia/Kolkata]")
@Api(value = "pets", description = "the pets API")
public interface PetsApi {
     * GET /pets : List all pets
     * @param limit How many items to return at one time (max 100) (optional)
     * @return A paged array of pets (status code 200)
     *         or unexpected error (status code 200)
    @ApiOperation(value = "List all pets", nickname = "listPets", notes = "", 
      response = Pet.class, responseContainer = "List", tags={ "pets", })
    @ApiResponses(value = { @ApiResponse(code = 200, message = "A paged array of pets", 
      response = Pet.class, responseContainer = "List"),
      @ApiResponse(code = 200, message = "unexpected error", response = Error.class) })
    @GetMapping(value = "/pets", produces = { "application/json" })
    default ResponseEntity<List> listPets(@ApiParam(
      value = "How many items to return at one time (max 100)") 
      @Valid @RequestParam(value = "limit", required = false) Integer limit) {
        return getDelegate().listPets(limit);

    // other generated methods

Second, since we're using the delegate pattern, OpenAPI also generates a delegator interface called PetsApiDelegate for us. In particular, methods declared in this interface return an HTTP status of 501 Not Implemented by default:

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", 
  date = "2021-03-22T23:26:32.308871+05:30[Asia/Kolkata]")
public interface PetsApiDelegate {
     * GET /pets : List all pets
     * @param limit How many items to return at one time (max 100) (optional)
     * @return A paged array of pets (status code 200)
     *         or unexpected error (status code 200)
     * @see PetsApi#listPets
    default ResponseEntity<List<Pet>> listPets(Integer limit) {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "{ \"name\" : \"name\", \"id\" : 0, \"tag\" : \"tag\" }";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString);
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    // other generated method declarations

After that, we see there's a PetsApiController class, that simply wires in the delegator:

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", 
  date = "2021-03-22T23:26:32.308871+05:30[Asia/Kolkata]")
public class PetsApiController implements PetsApi {

    private final PetsApiDelegate delegate;

    public PetsApiController(
      @org.springframework.beans.factory.annotation.Autowired(required = false) PetsApiDelegate delegate) {
        this.delegate = Optional.ofNullable(delegate).orElse(new PetsApiDelegate() {});

    public PetsApiDelegate getDelegate() {
        return delegate;

In the modelPackage, a couple of data model POJOs called Error and Pet are generated, based on the schemas defined in our YAML input.

Let's look at one of them – Pet:

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", 
  date = "2021-03-22T23:26:32.308871+05:30[Asia/Kolkata]")
public class Pet {
  private Long id;

  private String name;

  private String tag;

  // constructor

  @ApiModelProperty(required = true, value = "")
  public Long getId() {
    return id;

  // other getters and setters

  // equals, hashcode, and toString methods

5. Testing the Server

Now all that is required for the server stub to be functional as a server, is to add an implementation of the delegator interface.

To keep things simple, here we won't do that and only test the stub. Moreover, before doing that we'll need a Spring Application:

public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

5.1. Test Using curl

After starting up the application, we'll simply run the command:

curl -I http://localhost:8080/pets/

And here's the expected result:

HTTP/1.1 501 
Content-Length: 0
Date: Fri, 26 Mar 2021 17:29:25 GMT
Connection: close

5.2. Integration Tests

Alternatively, we can write a simple integration test for the same:

public class OpenApiPetsIntegrationTest {
    private static final String PETS_PATH = "/pets/";

    private MockMvc mockMvc;

    public void whenReadAll_thenStatusIsNotImplemented() throws Exception {

    public void whenReadOne_thenStatusIsNotImplemented() throws Exception {
        this.mockMvc.perform(get(PETS_PATH + 1)).andExpect(status().isNotImplemented());

6. Conclusion

In this tutorial, we saw how to generate a Spring-based server stub from a YAML specification using the OpenAPI generator's maven plugin.

As a next step, we can also use it to generate a client.

As always, source code is available over on GitHub.

Spring bottom

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

Java bottom

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

Inline Feedbacks
View all comments