Writing API definition is pretty cool stuff. It helps consumers to understand the API and agree on its attributes. In our company for that purpose we are using OpenAPI Specification (formerly Swagger Specification).
But the real deal is generating code and documentation from the specification file. In this blog, I will show you how we are doing that at N47.
We will split this blog into two parts. The first part will be generating code, and the second part will be using the generated code.
Part 1
We are creating an empty maven project named “demo-specification”.
Next thing is creating an API definition file, api.yaml in src/main/resources/ directory. The demo content of this file is:
openapi: "3.0.0"
info:
description: "Codegen for demo service"
version: "0.0.1"
title: "Demo Service Specification"
contact:
email: "antonie.zafirov@north-47.com"
tags:
- name: "user"
description: "User tag for demo purposes"
servers:
- url: http://localhost:8000/
description: "local host"
paths:
/user/{id}:
get:
tags:
- "user"
summary: "Retrieves User by ID"
operationId: "getUserById"
parameters:
- name: "id"
in: "path"
description: "retrieves user by user id"
required: true
schema:
type: "integer"
format: "int64"
responses:
200:
description: "Retrieves family members by person id"
content:
application/json:
schema:
type: "object"
$ref: '#/components/schemas/User'
components:
schemas:
User:
type: "object"
required:
- "id"
- "firstName"
- "lastName"
- "dateOfBirth"
- "gender"
properties:
id:
type: "integer"
format: "int64"
firstName:
type: "string"
example: "John"
lastName:
type: "string"
example: "Smith"
dateOfBirth:
type: "string"
example: "1992-10-05"
gender:
type: "string"
enum:
- "MALE"
- "FEMALE"
- "UNKNOWN"
Next step is updating pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.47northlabs</groupId>
<artifactId>demo-specification</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<swagger-annotations-version>1.5.22</swagger-annotations-version>
<jersey-version>2.27</jersey-version>
<jackson-version>2.8.9</jackson-version>
<jodatime-version>2.7</jodatime-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.8.1</junit-version>
<springfox-version>2.9.2</springfox-version>
<threetenbp-version>1.3.8</threetenbp-version>
<datatype-threetenbp-version>2.6.4</datatype-threetenbp-version>
<spring-boot-starter-test-version>2.1.1.RELEASE</spring-boot-starter-test-version>
<spring-boot-starter-web-version>2.1.0.RELEASE</spring-boot-starter-web-version>
<junit-version>4.12</junit-version>
<migbase64-version>2.2</migbase64-version>
</properties>
<dependencies>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-base</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${jodatime-version}</version>
</dependency>
<dependency>
<groupId>com.brsanthu</groupId>
<artifactId>migbase64</artifactId>
<version>${migbase64-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-starter-test-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot-starter-web-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>${threetenbp-version}</version>
</dependency>
<dependency>
<groupId>com.github.joschi.jackson</groupId>
<artifactId>jackson-datatype-threetenbp</artifactId>
<version>${datatype-threetenbp-version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>3.3.4</version>
<executions>
<execution>
<id>spring-boot-api</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
<generatorName>spring</generatorName>
<configOptions>
<dateLibrary>joda</dateLibrary>
</configOptions>
<library>spring-boot</library>
<apiPackage>com.northlabs.demo.api</apiPackage>
<modelPackage>com.northlabs.demo.api.model</modelPackage>
<invokerPackage>com.northlabs.demo.api.handler</invokerPackage>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
After that, we are executing MVN clean install in the root directory of the project. The result is in target/generated-sources/. com.northlabs.demo.api.UserApi generated API interface is what we need.
The magic is done by openapi-generator-maven-plugin. There are a lot of different generators that can be used, with a lot of options. Here is the list of them.
Part 2
Let’s create a new spring boot project demo-service from https://start.spring.io/.
What we need to do is to add demo-specification as a maven dependency in the demo-service project.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.47northlabs</groupId>
<artifactId>demo-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.47northlabs</groupId>
<artifactId>demo-specification</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
In application.properties file we are setting server.port to 8000.
server.port=8000
Next step is creating a class UserRestController which will implement previously generated UserApi from demo-specification.
package com.northlabs.demoservice.rest.controller;
import com.northlabs.demo.api.UserApi;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserRestController implements UserApi {
}
Now, if we run the application and try to make GET request to /user/1 the response status will be 501 Not Implemented.
Let’s make some simple implementation of the API:
package com.northlabs.demoservice.rest.controller;
import com.northlabs.demo.api.UserApi;
import com.northlabs.demo.api.model.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserRestController implements UserApi {
@Override
public ResponseEntity<User> getUserById(@PathVariable("id") Long id) {
User user = new User();
user.setId(id);
user.setFirstName("John");
user.setLastName("Doe");
user.setGender(User.GenderEnum.MALE);
user.setDateOfBirth("01-01-1970");
return ResponseEntity.ok(user);
}
}
Now the response will be:
And we are done!
This is how we are implementing OpenAPI/Swagger in our projects.
In the next blog, I will show you how you can provide Swagger UI, generate Java client, JavaScript client modify base paths etc.
Download the source code
Both projects are freely available on our GitLab repository. Feel free to fix any mistakes and to comment here if you have any questions or feedback.
https://gitlab.com/47northlabs/public/openapi-codegen-demo/demo-specification
https://gitlab.com/47northlabs/public/openapi-codegen-demo/demo-service
Great 🙂 exactly what I needed.
Unfortunately it doesn’t work for me – I’m trying to run it but the demo-service application give me “Whitelabel Error Page”
Also before I provide the implementation for the method.
I tried to use the API dependency as a fat jar, but still no success.
However it’s great to illustrate the concept.
As for me I’m still trying to make it run – if you can help it’ll be very nice…
🙂 🙂 🙂
Ignore the previous – it was stupid typo in a package name – all works like a charm!
One more time – great demo!
Hey Ivan, how did you solve this, I am also getting this error.
How can we pass the Pagination details ? Is there is way to pass the User provided pagination information?
It looks like there is an x-spring-paginated extension PR that was never merged [https://github.com/swagger-api/swagger-codegen/pull/3357]. As a workaround, you can follow the implementation suggested here
[https://github.com/swagger-api/swagger-codegen/issues/5506#issuecomment-509506567]. To define query parameters and encapsulate your paginated response.
hi,
after userapi create class, method name is starting default. (default ResponseEntity getUserById()….
I dont want to start the default. How can I remove ?
Hi Jeniffer,
You can avoid default in front of the method name with adding
<java8>false</java8>
in configOptions.BR,
Antonie
Great tutorial, thank you. But how can i split 1 yaml file spec. to multiple and make it works (Didnt find any way how to do that) ? (To save Single Respon. Principle)
Hi Andrii,
You can split your yaml file by using $ref, take a look in the documentation https://swagger.io/docs/specification/using-ref/