Friday, July 16, 2021

Creating springboot project from swagger codegen jar files using yaml files

 In past below article we understood how we can use the plugin in pom.xml to generate the springboot code.


http://siddharathadhumale.blogspot.com/2021/07/swagger-codegen-sprintboot-example.html

Now lets check the same using jar files

We will generate and test the server code. i.e. creating new springboot application from the YAML files.

First download the required swagger code gen jar

swagger-codegen-cli-3.0.27.jar
Now lets say we have our Employee.yaml files and want to generate the Springboot sever stub for this files. For that use the belwo command.

For details please use belwo url.
https://github.com/swagger-api/swagger-codegen#generating-libraries-from-your-server

1
java -jar swagger-codegen-cli-3.0.27.jar generate -i C:/Swagger_API/Employee.yaml -l spring  -o C:/Swagger_API/generated-sources/server/swagger

or

1
2
3
4
5
6
7
8
9
java -jar swagger-codegen-cli-2.3.1.jar generate \
  -i swagger.yaml \
  --api-package com.siddhu.api \
  --model-package com.siddhu.employee.model \
  --group-id com.siddhu \
  --artifact-id spring-swagger-codegen-employee \
  --artifact-version 0.0.1-SNAPSHOT \
  -l spring \ 
  -o C:/Swagger_API/generated-sources/server/swagger

Excute this command on the prompt

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
C:\Swagger_API>java -jar swagger-codegen-cli-3.0.27.jar generate -i C:/Swagger_API/Employee.yaml -l spring  -o C:/Swagger_API/generated-sources/server/swagger
16:48:32.464 [Thread-0] WARN  i.s.c.v.i.CodegenIgnoreProcessor - Output directory does not exist, or is inaccessible. No file (.swagger-codegen-ignore) will be evaluated.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.github.jknack.handlebars.context.MemberValueResolver (file:/C:/Swagger_API/swagger-codegen-cli-3.0.27.jar) to method java.util.Collections$EmptyMap.isEmpty()
WARNING: Please consider reporting this to the maintainers of com.github.jknack.handlebars.context.MemberValueResolver
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
16:48:32.911 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\model\Employee.java
16:48:33.131 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\model\ModelAndView.java
16:48:33.211 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\model\View.java
16:48:33.433 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\DeleteEmployeeApiController.java
16:48:33.565 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\DeleteEmployeeApi.java
16:48:33.661 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\ErrorApiController.java
16:48:33.714 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\ErrorApi.java
16:48:33.760 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\GetEmployeeApiController.java
16:48:33.822 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\GetEmployeeApi.java
16:48:33.948 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\InsertEmployeeApiController.java
16:48:34.048 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\InsertEmployeeApi.java
16:48:34.147 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\UpdateEmployeeApiController.java
16:48:34.229 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\UpdateEmployeeApi.java
16:48:34.245 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\pom.xml
16:48:34.254 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\README.md
16:48:34.258 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\configuration\HomeController.java
16:48:34.264 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\Swagger2SpringBoot.java
16:48:34.269 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\RFC3339DateFormat.java
16:48:34.324 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\configuration\SwaggerUiConfiguration.java
16:48:34.328 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\resources\application.properties
16:48:34.332 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\ApiException.java
16:48:34.337 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\ApiResponseMessage.java
16:48:34.342 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\NotFoundException.java
16:48:34.350 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\api\ApiOriginFilter.java
16:48:34.362 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\configuration\SwaggerDocumentationConfig.java
16:48:34.395 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\configuration\CustomInstantDeserializer.java
16:48:34.399 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\src\main\java\io\swagger\configuration\JacksonConfiguration.java
16:48:34.423 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\.swagger-codegen-ignore
16:48:34.436 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\generated-sources\server\swagger\.swagger-codegen\VERSION
 
C:\Swagger_API>

You will be able to see our springboot server stub is created in side C:\Swagger_API

Import that code in your springboot application and modify you pom.xml as per pom.xml give inside this folder C:\Swagger_API\generated-sources\server\swagger and you are good to go.

Similarly we had crated our Feign client code using plugin in the below blog

http://siddharathadhumale.blogspot.com/2021/07/creating-spring-boot-feign-client-code.html


in same we can also generate the Client code. i.e. client can be feign or resttemplate etc.

For more information refer to the below url:-
https://github.com/swagger-api/swagger-codegen#to-generate-a-sample-client-library

Use below comment to generate the feign client code from given yaml files.

1
2
3
4
5
6
7
8
java -jar swagger-codegen-cli-3.0.27.jar generate -i C:/Swagger_API/Employee.yaml -l spring --library spring-cloud -o C:/Swagger_API/client-generated-sources/server/swagger
 
 
java -jar swagger-codegen-cli-3.0.27.jar generate \
     -l spring \
     --library spring-cloud \
     -o c://generated-sources//swagger

or

1
2
3
4
5
6
7
8
9
10
java -jar swagger-codegen-cli-2.3.1.jar generate \
  -i swagger.yaml \
  --api-package com.siddhu.api \
  --model-package com.siddhu.employee.model \
  --group-id com.siddhu \
  --artifact-id spring-swagger-codegen-employee \
  --artifact-version 0.0.1-SNAPSHOT \
  -l spring \
  --library spring-cloud
  -o C:/Swagger_API/client-generated-sources/server/swagger

Lets try to run the command

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
C:\Swagger_API>java -jar swagger-codegen-cli-3.0.27.jar generate -i C:/Swagger_API/Employee.yaml -l spring --library spring-cloud -o C:/Swagger_API/client-generated-sources/server/swagger
16:55:35.679 [Thread-0] WARN  i.s.c.v.i.CodegenIgnoreProcessor - Output directory does not exist, or is inaccessible. No file (.swagger-codegen-ignore) will be evaluated.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.github.jknack.handlebars.context.MemberValueResolver (file:/C:/Swagger_API/swagger-codegen-cli-3.0.27.jar) to method java.util.Collections$EmptyMap.isEmpty()
WARNING: Please consider reporting this to the maintainers of com.github.jknack.handlebars.context.MemberValueResolver
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
16:55:36.117 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\main\java\io\swagger\model\Employee.java
16:55:36.346 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\main\java\io\swagger\model\ModelAndView.java
16:55:36.424 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\main\java\io\swagger\model\View.java
16:55:36.637 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\main\java\io\swagger\api\BasicErrorControllerApi.java
16:55:36.646 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\main\java\io\swagger\api\BasicErrorControllerApiClient.java
16:55:36.904 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\main\java\io\swagger\api\IndexControllerApi.java
16:55:36.912 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\main\java\io\swagger\api\IndexControllerApiClient.java
16:55:36.938 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\pom.xml
16:55:36.950 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\README.md
16:55:36.955 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\main\java\io\swagger\configuration\ApiKeyRequestInterceptor.java
16:55:36.977 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\main\java\io\swagger\configuration\ClientConfiguration.java
16:55:36.981 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\test\resources\application.yml
16:55:36.989 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\test\java\io\swagger\TestUtils.java
16:55:36.995 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\test\java\io\swagger\Application.java
16:55:37.044 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\main\java\io\swagger\configuration\CustomInstantDeserializer.java
16:55:37.050 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\src\main\java\io\swagger\configuration\JacksonConfiguration.java
16:55:37.058 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\.swagger-codegen-ignore
16:55:37.060 [Thread-0] INFO  i.s.codegen.v3.AbstractGenerator - writing file C:\Swagger_API\client-generated-sources\server\swagger\.swagger-codegen\VERSION
 
C:\Swagger_API>

You will be able to see our springboot Feign client stub is created in side C:\Swagger_API

Import that code in your springboot application and modify your pom.xml as per give pom.xml inside this folder C:\Swagger_API\client-generated-sources\server\swagger, and you are good to go.

In addition, we also have other flavor of Swagger such as
Swagger Editor– To edit the yaml and json file and check the documentation run time.
Swagger Parser – Use to parse the yaml or json files at run time i.e. to check the docs internal items.
Swagger Core – This can be used to create the OpenAPI3 specification yaml or jason file for JAX-RS implemented project i.e. jersey or Resteasy etc.
Swagger Inspector – use to create the OpenPAI 3 specification documentation and json or yaml files for any rest url
Swagger Hub – It is combination of Swagger Editor + Swagger CodeGen. If we provide the json or yaml files then we can use this tool for creating client i.e. feign client in java spring boot etc or server stub such as spring boot example etc.

reference :- https://swagger.io/blog/api-strategy/difference-between-swagger-and-openapi/

No comments: