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 git@gitee.com: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,谢谢!

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值