How to automatically generate interface and Data Transfer Object from OpenAPI yaml file.
OpenAPI yaml file
I put the OpenAPI specification file in specs/openapi.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
openapi:3.0.3 info: title:goodsservice version:1.0.0 description:|- This is a goods service Provide price query and inventory management functions license: name:Apache2.0 url:'https://www.apache.org/licenses/LICENSE-2.0' contact: name:samzhu url:'https://blog.samzhu.dev/' email:spike19820318@gmail.com
I will use the gradle plugin to generate code, but only supports up to 3.0.3 specifications.
Add gradle plugin
org.openapi.generator Currently plugin only supports 6.9, 7.0 is a problem, you can use the following commands to adjust.
1
./gradlew wrapper --gradle-version 6.9
build.gradle add
1 2 3
plugins { id 'org.openapi.generator' version '5.1.1' }
The above is roughly finished, and finally you really want to generate it directly into the project, you can directly change openapiOutputDir to the project directory
> Task :openApiValidate Validating spec /Users/samzhu/workspace/cloud-technology/goods-service/specs/openapi.yaml Spec is valid.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/6.9/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1s 1 actionable task: 1 executed
> Task :openApiGenerate ################################################################################ # Thanks for using OpenAPI Generator. # # Please consider donation to help us maintain this project π # # https://opencollective.com/openapi_generator/donate # ################################################################################ Successfully generated code to /Users/samzhu/workspace/cloud-technology/goods-service/generated/springboot
BUILD SUCCESSFUL in 1s 1 actionable task: 1 executed
> Task :buildFeignSDK ################################################################################ # Thanks for using OpenAPI Generator. # # Please consider donation to help us maintain this project π # # https://opencollective.com/openapi_generator/donate # ################################################################################ Successfully generated code to /Users/samzhu/workspace/cloud-technology/goods-service/generated/client/feign
> Task :buildAngularSdk ################################################################################ # Thanks for using OpenAPI Generator. # # Please consider donation to help us maintain this project π # # https://opencollective.com/openapi_generator/donate # ################################################################################ Successfully generated code to /Users/samzhu/workspace/cloud-technology/goods-service/generated/client/angular
> Task :buildGoSdk ################################################################################ # Thanks for using OpenAPI Generator. # # Please consider donation to help us maintain this project π # # https://opencollective.com/openapi_generator/donate # ################################################################################ Successfully generated code to /Users/samzhu/workspace/cloud-technology/goods-service/generated/client/go
BUILD SUCCESSFUL in 1s 3 actionable tasks: 3 executed
Then you will get the result.
Customization
In template/pojo.mustache I have customized to add java annotations.
1 2 3 4 5 6 7 8 9
{{#vendorExtensions.x-java-class-annotations}} {{.}} {{/vendorExtensions.x-java-class-annotations}} public class {{classname}} {{#parent}}extends ....{
If it is generated to the root directory of the project, there will be more files that I donβt need. At this time, I can exclude them through .openapi-generator-ignore, I will exclude the pom.xml & README.md that I donβt need. Examples are as follows
.openapi-generator-ignore
1 2 3
# add generator exclude pom.xml README.md
Because usually projects have their own custom configuration, so I set interfaceOnly to true, so that only controller interface and dto will be generated. If you are used to generating a complete project through the generator, you can try setting it to false.