swagger2-plus,支持使用註解排除參數,基於springfox-swagger2:2.8.0版本

介紹

項目地址

使用方法

直接使用

項目已經發布到maven中央倉庫,直接在pom.xml中引用即可

<dependencies>
    <dependency>
        <groupId>com.xzixi</groupId>
        <artifactId>swagger2-plus</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

修改後使用

  1. 下載項目

    打開git bash窗口,執行命令git clone [email protected]:xuelingkang/swagger2.git

  2. 編譯並安裝到本地maven倉庫

    進入工程目錄,打開cmd窗口,執行命令mvn clean install -Dmaven.test.skip=true

  3. 在自己的項目中引用

<dependencies>
    <dependency>
        <groupId>com.xzixi</groupId>
        <artifactId>swagger2-plus</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

配置類

所有swagger2的配置都不用變,只需要將註解替換掉

package com.xzixi.swagger2.plus.demo.config;

import com.xzixi.swagger2.plus.annotation.EnableSwagger2Plus;
import io.swagger.annotations.Api;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@ConditionalOnExpression("${swagger2.enable}==true")
@EnableSwagger2Plus // 只需要將@EnableSwagger2替換成@EnableSwagger2Plus即可
public class Swagger2Config {

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) // 只顯示添加@Api註解的類
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .title("swagger2-plus演示案例")
                        .version("1.0")
                        .build());
    }

}

IgnoreSwagger2Parameter註解

主要用到的類

package com.xzixi.swagger2.plus.demo.controller;

import com.xzixi.swagger2.plus.demo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "demo", produces = "application/json; charset=UTF-8")
@Api(tags = "用戶相關api")
public class UserController {

    @GetMapping
    @ApiOperation(value = "測試")
    public String test(User user) {
        System.out.println(user);
        return "隨便返回點什麼";
    }

}
package com.xzixi.swagger2.plus.demo.entity;

import com.xzixi.swagger2.plus.annotation.IgnoreSwagger2Parameter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value = "用戶")
public class User {

    @ApiModelProperty(value = "姓名")
    private String name;
    @ApiModelProperty(value = "性別")
    private String sex;
    @ApiModelProperty(value = "部門")
    // 假設這個屬性是我們不希望在swagger2文檔頁面顯示的參數
    private Dept dept;

}
package com.xzixi.swagger2.plus.demo.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value = "部門")
public class Dept {

    @ApiModelProperty(value = "部門編號")
    private String deptNo;
    @ApiModelProperty(value = "部門名稱")
    private String deptName;

}
  • 先看一下不使用IgnoreSwagger2Parameter註解的效果

不使用IgnoreSwagger2Parameter註解的效果

  • 再看看使用IgnoreSwagger2Parameter註解的效果

先修改User類

package com.xzixi.swagger2.plus.demo.entity;

import com.xzixi.swagger2.plus.annotation.IgnoreSwagger2Parameter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value = "用戶")
public class User {

    @ApiModelProperty(value = "姓名")
    private String name;
    @ApiModelProperty(value = "性別")
    private String sex;
    @ApiModelProperty(value = "部門")
    @IgnoreSwagger2Parameter // 只需要添加註解就可以在文檔中排除參數
    private Dept dept;

}

使用IgnoreSwagger2Parameter註解的效果

詳細使用方法請參考示例工程swagger2-plus-demo

歡迎提出寶貴意見

如果我的代碼對您有幫助,希望給我個star,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章