Spring Boot With OpenApi#
What Is The OpenApi?#
-
The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to RESTful APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined, a consumer can understand and interact with the remote service with a minimal amount of implementation logic.
-
An OpenAPI definition can then be used by documentation generation tools to display the API, code generation tools to generate servers and clients in various programming languages, testing tools, and many other use cases.
- More Information.
Dependencies And Plugins#
- You need to import some dependencies below into
pom.xmlof your spring boot project for needed imports, validation annotations and OpenApi UI for your generated classes.
| pom.xml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
- Then for the OpenApi Generator, let's put the plugin below into your
pom.xmlwith some configurations.
| pom.xml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
OpenApi Implementation And Configuration#
- Now let create swagger package in the
resourcesfolder and createswagger-input.ymlwith your api definitions. You can use swagger editor to write your api definitions easily. The example swagger yml will look like below.
| swagger-input.yml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
Testing#
- Now, you will use command
mvn clean packageto build your project with swagger then you will see the generated source as in the image below
- Now, let create a
ServerControllercomponent and implementV1Apifrom generated source, all your apis was generated with validations following your openapi specification, so you just need implement the generated api for using.
| ServerController.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
- You can see full source code here which includes samples for,
RepositoryEntityandService. Because we just focus on OpenApi so we will not put these implementation details here. - Now let's start your Spring Boot service and go to
http://localhost:8080/swagger-ui/index.htmlto check the openapi UI.
- You can also execute api on the openapi ui directly instead of using postman to execute apis.
Advances#
- By default, OpenApi will automatically scan all your controllers in project and generate all UIs for them. However, in some case you want to hide your sensitive apis from OpenApi UI, you can create a configuration class with content as below.
| OpenApiConfig.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
- In which, only api paths that matched in the
pathsToMatchconfiguration will be generated on the UI. Other api paths will not generated. The UI now will look like below with selectedgroupthat you configured.
- Sometimes, you want to take more controls on openapi generator like using Java Instant for generated models then you can add more configuration option as below.
| pom.xml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
templates package in your resources directory. Then in your pom.xml add templateDirectory tag with the path to your custom templates. Your plugin will look like below.
| pom.xml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
- Then add your custom template
mustachesfile there. Note the name of mustaches files have to be the same as in this repository. - You can copy the content of the mustaches files as in the repo above and edit the content as the way you want.
- For example, I want to add
@JsonIgnorePropertiesinto every generated models. So I can do it as the image below.
- Then build your project again and check your generated models, you can see the annotation as below





