• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Spring Boot配置Swagger增强工具

武飞扬头像
老徐··
帮助1

参考knife4j官方网站:1.6 快速开始 | knife4j

一、话不多说,先看效果

1.Swagger页面这样,这里就不多做介绍

学新通

2.增强后的首页

学新通

 3.API列表

学新通

4.API请求参数

学新通

5.API响应参数

学新通

个人觉得增强后看着很清晰,比原来的要好很多。

二、开始配置 

1.pom.xml 配置

  1.  
    <dependency>
  2.  
    <groupId>io.springfox</groupId>
  3.  
    <artifactId>springfox-swagger2</artifactId>
  4.  
    <version>2.10.5</version>
  5.  
    </dependency>
  6.  
    <dependency>
  7.  
    <groupId>io.springfox</groupId>
  8.  
    <artifactId>springfox-swagger-ui</artifactId>
  9.  
    <version>2.9.2</version>
  10.  
    </dependency>
  11.  
    <dependency>
  12.  
    <groupId>com.github.xiaoymin</groupId>
  13.  
    <artifactId>knife4j-spring-boot-starter</artifactId>
  14.  
    <version>2.0.7</version>
  15.  
    </dependency>
学新通
配置中遇到的问题:
springfox-swagger2这个的版本号不同会导致无法导入@EnableSwagger2WebMvc这个注解

2.配置Swagger2Config

  1.  
     
  2.  
    import org.springframework.context.annotation.Bean;
  3.  
    import org.springframework.context.annotation.Configuration;
  4.  
    import springfox.documentation.builders.ApiInfoBuilder;
  5.  
    import springfox.documentation.builders.PathSelectors;
  6.  
    import springfox.documentation.builders.RequestHandlerSelectors;
  7.  
    import springfox.documentation.service.ApiInfo;
  8.  
    import springfox.documentation.service.Contact;
  9.  
    import springfox.documentation.spi.DocumentationType;
  10.  
    import springfox.documentation.spring.web.plugins.Docket;
  11.  
    import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
  12.  
     
  13.  
    /**
  14.  
    *@类名 Swagger2Config
  15.  
    *@描述 配置Swagger2
  16.  
    *@版本 1.0
  17.  
    *@创建人 XuKang
  18.  
    *@创建时间 2021/7/16 13:29
  19.  
    **/
  20.  
    @Configuration
  21.  
    @EnableSwagger2WebMvc
  22.  
    public class Swagger2Config {
  23.  
     
  24.  
    /**
  25.  
    * @method createRestApi
  26.  
    * @desc 配置SWAGGER参数
  27.  
    * @version V1.0.0
  28.  
    * @author xukang
  29.  
    * @date 2021/7/16 13:30
  30.  
    * @return
  31.  
    */
  32.  
    @Bean
  33.  
    public Docket createRestApi() {
  34.  
    return new Docket(DocumentationType.SWAGGER_2)
  35.  
    .apiInfo(apiInfo())
  36.  
    .groupName("test")//分组名
  37.  
    .select()
  38.  
    .apis(RequestHandlerSelectors.basePackage("com.demo.controller"))//定位到自己的接口所在包的路径
  39.  
    .paths(PathSelectors.any())
  40.  
    .build();
  41.  
    }
  42.  
     
  43.  
    /**
  44.  
    * @method apiInfo
  45.  
    * @desc 配置ApiInfo
  46.  
    * @version V1.0.0
  47.  
    * @author xukang
  48.  
    * @date 2021/7/16 13:30
  49.  
    * @return ApiInfo
  50.  
    */
  51.  
    private ApiInfo apiInfo() {
  52.  
    return new ApiInfoBuilder()
  53.  
    //.title("Spring Boot中使用Swagger2构建RESTful APIs")
  54.  
    .title("标题")
  55.  
    .description("简介")
  56.  
    .termsOfServiceUrl("http://localhost:8080")
  57.  
    .contact(new Contact("作者名称","http://localhost:8080","123@163.com"))
  58.  
    .version("1.0")
  59.  
    .build();
  60.  
    }
  61.  
     
  62.  
    }
学新通

 3.配置WebMvcConfig

  1.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  2.  
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  3.  
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  4.  
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  5.  
    import springfox.documentation.spring.web.SpringfoxWebMvcConfiguration;
  6.  
     
  7.  
    /**
  8.  
    *@类名 WebMvcConfig
  9.  
    *@描述 文件配置
  10.  
    *@版本 1.0
  11.  
    *@创建人 XuKang
  12.  
    *@创建时间 2021/12/28 14:22
  13.  
    **/
  14.  
    @SpringBootApplication
  15.  
    @ConditionalOnClass(SpringfoxWebMvcConfiguration.class)
  16.  
    public class WebMvcConfig implements WebMvcConfigurer {
  17.  
     
  18.  
    @Override
  19.  
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
  20.  
    registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
  21.  
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  22.  
    }
  23.  
    }
学新通

4.开发环境yml文件可以打开增强配置

打开之后可以根据官网自定义一下组件,例如排序等

  1.  
    knife4j:
  2.  
    enable: true #开启配置增强
  3.  
    setting:
  4.  
    enableVersion: true

5.生产环境yml文件可以屏蔽

  1.  
    knife4j:
  2.  
    enable: true #开启配置增强
  3.  
    # 开启Swagger的Basic认证功能,默认是false
  4.  
    # 开启生产环境屏蔽
  5.  
    production: true

6.controller注解配置

  1.  
    @RestController
  2.  
    @RequestMapping("wechat/bank/")
  3.  
    @Api(value = "微信银行所用工具相关组件", tags = "3.微信微信银行相关组件")
  4.  
    public class LkWeChatBankController{
  5.  
    }

对应的效果如下

学新通

7.entity实体类配置

  1.  
    import com.alibaba.fastjson.annotation.JSONField;
  2.  
    import com.jingmai.video.live.pay.providers.model.vo.entity.LkWeChatBanksBranchesData;
  3.  
    import com.jingmai.video.live.pay.providers.model.vo.entity.LkWeChatLinksVO;
  4.  
    import io.swagger.annotations.ApiModel;
  5.  
    import io.swagger.annotations.ApiModelProperty;
  6.  
    import lombok.Data;
  7.  
     
  8.  
    import java.io.Serializable;
  9.  
    import java.util.List;
  10.  
     
  11.  
    /**
  12.  
    * @类名 LkWeChatBanksBranchesResponseVO
  13.  
    * @描述 查询支行列表响应实体
  14.  
    * @版本 1.0
  15.  
    * @创建人 XuKang
  16.  
    * @创建时间 2022/1/12 14:00
  17.  
    **/
  18.  
    @Data
  19.  
    @ApiModel(value="LkWeChatBanksBranchesResponseVO",description="查询支行列表响应实体")
  20.  
    public class LkWeChatBanksBranchesResponseVO implements Serializable {
  21.  
     
  22.  
    private static final long serialVersionUID = -7098096518017171297L;
  23.  
     
  24.  
    @JSONField(name = "total_count")
  25.  
    @ApiModelProperty(value = "查询数据总条数",name = "totalCount",example = "10",required=true,position = 1)
  26.  
    private int totalCount;
  27.  
     
  28.  
    @ApiModelProperty(value = "本次查询数据条数",name = "count",example = "10",required=true,position = 2)
  29.  
    private int count;
  30.  
     
  31.  
    @ApiModelProperty(value = "-支行列表",name = "data",required=false,position = 3)
  32.  
    private List<LkWeChatBanksBranchesData> data;
  33.  
     
  34.  
    @ApiModelProperty(value = "本次查询偏移量",name = "offset",example = "0",required=true,position = 4)
  35.  
    private String offset;
  36.  
     
  37.  
    @ApiModelProperty(value = "分页链接",name = "links",required=true,position = 5)
  38.  
    private LkWeChatLinksVO links;
  39.  
     
  40.  
    @JSONField(name = "account_bank")
  41.  
    @ApiModelProperty(value = "开户银行",name = "accountBank",example = "招商银行其他银行",required=true, position = 6)
  42.  
    private String accountBank;
  43.  
     
  44.  
    @JSONField(name = "account_bank_code")
  45.  
    @ApiModelProperty(value = "开户银行编码",name = "accountBankCode",example = "1001",required=true, position = 7)
  46.  
    private int accountBankCode;
  47.  
     
  48.  
    @JSONField(name = "bank_alias")
  49.  
    @ApiModelProperty(value = "银行别名",name = "bankAlias",example = "工商银行深圳前海微众银行",required=true, position = 8)
  50.  
    private String bankAlias;
  51.  
     
  52.  
    @JSONField(name = "bank_alias_code")
  53.  
    @ApiModelProperty(value = "银行别名编码",name = "bankAliasCode",example = "1000006247",required=true, position = 9)
  54.  
    private String bankAliasCode;
  55.  
     
  56.  
    }
学新通
@ApiModel注解:
value:实体value,在写接口的和@ApiImplicitParam(dataType={})对应
description:实体简介
@ApiModelProperty注解:

value:字段说明

name:对应的字段

example:示例值

required:是否必填

position:排序

8.Controller方法配置

  1.  
    @PostMapping("getBankingBranches/{bankAliasCode}")
  2.  
    @ApiOperationSupport(order = 5)
  3.  
    @ApiOperation(value="查询支行列表", notes="本接口可以用于根据银行别名编码(仅支持需要填写支行的银行别名编码)和城市编码过滤查询支行列表数据")
  4.  
    @ApiImplicitParams({
  5.  
    @ApiImplicitParam(name = "weChatBanksBranchesRequestVO", value = "查询支行列表请求实体", required = true, dataType = "LkWeChatBanksBranchesRequestVO"),
  6.  
    @ApiImplicitParam(name = "bankAliasCode", value = "银行别名编码", required = true,dataType = "String",example = "1000006247",paramType = "path")
  7.  
    })
  8.  
    public Result<LkWeChatBanksBranchesResponseVO> getBankingBranches(@RequestBody LkWeChatBanksBranchesRequestVO weChatBanksBranchesRequestVO,
  9.  
    @PathVariable(value = "bankAliasCode") String bankAliasCode){
  10.  
    return null;
@ApiOperationSupport:排序

别的注解应该可以看懂,自己看吧

三、总结

总结个棒棒锤,自己去官网看,1.6 快速开始 | knife4j

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgefhba
系列文章
更多 icon
同类精品
更多 icon
继续加载