www.cognizant.com
Copyright © 2017 Cognizant
1
Spring boot REST data service code generation using Swagger code gen
Why use Automatic Code Generation?
Automatic Code Generation refers to using programs to generate
code that the user would otherwise have to write themselves.
Automatic code generation has certain advantages over traditional
coding.
• Correctness
• High quality
• Efficient use of the resources
• Quick code generation
• Consistent
• Coding Abstractly
Two step approach.
Two step approach to generate the REST code using swagger code gen.
• Swagger code gen tool reads the table meta data details from DB
schema and generates the YAML file with Open API specs.
• It generates the REST code based on the YAML file.
Methodologies used to generate the code :
• Bottom up approach with online mode
Approach Diagram:
Important code blocks of REST Services to
be generated by the Swagger code gen.
Swagger Custom Code Gen:
Swagger Custom Code Gen:
• Default Swagger CodeGen generates only REST Controller of the
spring boot project. Swagger custom code generator implementation
is required to customize the REST controller, Service and DAO classes.
• Swagger custom CodeGen implementation contain following
important files:
• CustomTemplate Codegen file
• Mustache files
• Configuration files.
• Mustache file acts as templates for code generation.
Detailed steps to generate REST code.
• Deploy the YAML generator jar file to respective server.
• Post a request to the URL of YAML generator with schema name and table name.
o Sample URL: http://vdalhdpedged013.kbm1.loc:2020/codegen
o Input :Sample Request:
{
"schemaName":"CNTCT_PNT_CONSUMER",
"tableName":"CNTCT_PNT"
}
o Output : Swagger YAML file
• Deploy the Custom swagger code gen jar file to the respective server.
o Input : Swagger YAML file
o Include package structures and other configuration details as part of customConfig.json
o Execute the below command
o java -jar targetCustomTemplate-1.0-SNAPSHOT.jar generate -l spring -i swagger.yaml -o test -c customConfig.json
o Output : Spring boot REST data service code.
• Deploy generated Spring boot REST code and test the CURD operations of the Phoenix REST data services.
Next steps..
• Junit test cases implementation with good coverage.
• Preprocessor and post processes of request and response for processing.
• Client stub generation for given service through YAML.
• Open API documentation through YAML file.
• Error and Exception handling.
• REST wrapper generation for given soap code ( This is a huge
implementation).
• Different configuration files Implementation like security, environment
specific configuration (API gate way, service discovery configuration may
require in feature).
• Transaction management code implementation.
• Pom file generation based on dependencies.

Swagger codegen tool to generate REST services

  • 1.
    www.cognizant.com Copyright © 2017Cognizant 1 Spring boot REST data service code generation using Swagger code gen
  • 2.
    Why use AutomaticCode Generation? Automatic Code Generation refers to using programs to generate code that the user would otherwise have to write themselves. Automatic code generation has certain advantages over traditional coding. • Correctness • High quality • Efficient use of the resources • Quick code generation • Consistent • Coding Abstractly
  • 3.
    Two step approach. Twostep approach to generate the REST code using swagger code gen. • Swagger code gen tool reads the table meta data details from DB schema and generates the YAML file with Open API specs. • It generates the REST code based on the YAML file. Methodologies used to generate the code : • Bottom up approach with online mode
  • 4.
  • 5.
    Important code blocksof REST Services to be generated by the Swagger code gen.
  • 6.
  • 7.
    Swagger Custom CodeGen: • Default Swagger CodeGen generates only REST Controller of the spring boot project. Swagger custom code generator implementation is required to customize the REST controller, Service and DAO classes. • Swagger custom CodeGen implementation contain following important files: • CustomTemplate Codegen file • Mustache files • Configuration files. • Mustache file acts as templates for code generation.
  • 8.
    Detailed steps togenerate REST code. • Deploy the YAML generator jar file to respective server. • Post a request to the URL of YAML generator with schema name and table name. o Sample URL: http://vdalhdpedged013.kbm1.loc:2020/codegen o Input :Sample Request: { "schemaName":"CNTCT_PNT_CONSUMER", "tableName":"CNTCT_PNT" } o Output : Swagger YAML file • Deploy the Custom swagger code gen jar file to the respective server. o Input : Swagger YAML file o Include package structures and other configuration details as part of customConfig.json o Execute the below command o java -jar targetCustomTemplate-1.0-SNAPSHOT.jar generate -l spring -i swagger.yaml -o test -c customConfig.json o Output : Spring boot REST data service code. • Deploy generated Spring boot REST code and test the CURD operations of the Phoenix REST data services.
  • 9.
    Next steps.. • Junittest cases implementation with good coverage. • Preprocessor and post processes of request and response for processing. • Client stub generation for given service through YAML. • Open API documentation through YAML file. • Error and Exception handling. • REST wrapper generation for given soap code ( This is a huge implementation). • Different configuration files Implementation like security, environment specific configuration (API gate way, service discovery configuration may require in feature). • Transaction management code implementation. • Pom file generation based on dependencies.