Swagger codegen usage experience, open source plug-in transformation, and real-time generation of interface documents

Due to the rebuilding of the system at work, a set of framework functions was organized and developed according to the commission of the boss.

 

They are all old codes, so there are many pain points in the whole development cycle. I have solved two of them here! One is the communication in the front-end and back-end development process, and the unification of the pretext document in the entire life cycle, so swagger codegen is selected as the basis of the framework, and the modified swagger codegen plug-in is used to realize the automatic code generation, and the post-transformation pretext document generation plug-in is used. Realize the dynamic generation of excuse documents.

 

Reviews:

1. Reference the entities in the project basic package into the yaml file. The pom configuration file after the plug-in transformation

External references can be realized through the configuration of importMappings and typeMappings

Referenced directly in yaml via

$ref: "#/definitions/Prop"

for easy referencing.

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.4.13-pactera</version>
    <executions>
        <execution>
            <id>spring-server</id>
            <phase>pre-clean</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/api/##
                    tet121.yaml,
                    tet122.yaml,
                    tet123.yaml
                </inputSpec>

                <language>spring</language>
                <output>${project.basedir}</output>
                <apiPackage>test.controller</apiPackage>
                <modelPackage>test.controller.dto</modelPackage>
                <!-- 非诚重要的配置,否则文件将要被覆盖 -->
                <skipOverwrite>false</skipOverwrite>
                <importMappings>
                    <importMapping>TestBean=test.json.TestBean
                    </importMapping>

                </importMappings>
                <typeMappings>
                    <typeMapping>TestBean=test.json.TestBean</typeMapping>
                </typeMappings>
                <generateSupportingFiles>false</generateSupportingFiles>
                <configOptions>
                    <dateLibrary>java8</dateLibrary>
                    <java8>true</java8>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

2. How to implement inheritance in yaml

It can be realized by adding a discriminator in the definition process of definitons, so that the project can be introduced after the entity is defined externally. Subclasses can also easily use their defined implementation classes without being overridden.

Result:
  discriminator: "type"
  description: "统一的响应返回类型。"
  type: "object"

These two points are where I find it difficult to use. I checked a lot of information, but it was not easy to find. It is not easy to modify the plug-in. If you don't mind, please give me a thumbs up.

After the transformation, add the plug-in github address and deploy it to your own maven private server, so that the whole team can use it.

https://github.com/wang249639015/swagger-codegen.git

 

If you have any questions, you can add WeChat w249639015

 

 

This plug-in can make the generation of the entire excuse code unaware. The specific business personnel only need to pay attention to their own business logic implementation. If you are interested, you can also modify the ftl implementation of the plug-in during the generation process, and publish dubbo to automatically generate, sofa, etc. accomplish.

If you feel that this post and the plug-in have improved your work efficiency, I look forward to your rewards to help customize various plug-ins.

Your support is my driving force (one-click generation of pdf excuse documents, html excuse document tutorials, and follow-up.)

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_40669549/article/details/108125933