SpringBoot 使用OpenApi-generator插件,自动生成代码,简便开发,专注业务代码

使用 OpenApi-generator插件简便开发

在目前的项目一种使用到了 openapi-generator-maven-plugin 这个插件,于是就将这块剥离出来,尝试整合,挺成功的。接下来进入正文

pom文件依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>swagger</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>swagger</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>jackson-databind-nullable</artifactId>
            <version>0.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>spring-boot:run</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>4.0.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/swagger/api.yml</inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>com.example.swagger.rest</apiPackage>
                            <modelPackage>com.example.swagger.api.pojo</modelPackage>
                            <skipValidateSpec>false</skipValidateSpec>
                            <supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
                            <configOptions>
                                <delegatePattern>true</delegatePattern>
                                <title>swagger</title>
                                <serializableModel>true</serializableModel>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

本文的侧重pom依赖在这里:

        <dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>jackson-databind-nullable</artifactId>
            <version>0.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
    <build>
        <defaultGoal>spring-boot:run</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>4.0.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/swagger/api.yml</inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>com.example.swagger.rest</apiPackage>
                            <modelPackage>com.example.swagger.api.pojo</modelPackage>
                            <skipValidateSpec>false</skipValidateSpec>
                            <supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
                            <configOptions>
                                <delegatePattern>true</delegatePattern>
                                <title>swagger</title>
                                <serializableModel>true</serializableModel>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

侧重点这块主要在build里面的内容
inputSpec 指定了创建路径和xxx.yml文件(文件名称自己定,我用的是Api),这样就可以将生成的文件放在apiPackage和modelPackage指定的包路径下

inputSpec指定的文件内容如下 api.yml:

swagger: "2.0"
info:
  title: 'swagger test'
  description: ''
  version: '0.1'
  termsOfService: 'http://swagger.io/terms/'
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
schemes:
  - "http"
paths:
  /user/save:
    post:
      tags:
        - "User"
      summary: "save user"
      description: "return init password"
      operationId: saveUser
      produces:
        - "application/json"
      parameters:
        - in: body
          name: "user"
          required: true
          schema:
            $ref: "#/definitions/User"
      responses:
        200:
          description: "operate success"
          schema:
            type: object
            $ref: "#/definitions/Response"

definitions:
  Response:
    type: object
    properties:
      success:
        type: boolean
      code:
        type: string
      message:
        type: string
      data:
        type: object

  User:
    type: object
    required:
      - userNumber
      - userName
    properties:
      id:
        type: integer
        format: int64
      userNumber:
        type: string
        pattern: \d{8}
      userName:
        type: string
        pattern: ^[a-zA-Z]{1,40}$
      phone:
        type: string
        pattern: (\d{10})?
      userStatus:
        type: string
      operatorNumber:
        type: string

具体的api.yml书写格式可以去swagger里面看https://swagger.io/,这里不多做介绍。
我这里是通过在idea控制台Terminal输入命令: mvn generate-sources 对api.yml进行编译的,编译结果如下
在这里插入图片描述

编译后就生成了整个Controller和Service接口,还有实体类。我们只需要写一个实现类对生成的Service接口进行实现写业务层代码就ojbk了

在这里插入图片描述
个人感觉弄好之后,简便了很多开发的代码,挺实用的!!!!
具体代码在:https://gitee.com/mayliming/swagger.git

  • 9
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值