zoukankan      html  css  js  c++  java
  • Swagger 使用+入门

    Swagger 快速上手

    1. Maven 依赖如下

    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-swagger2</artifactId>
       <version>2.9.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-swagger-ui</artifactId>
       <version>2.9.2</version>
    </dependency>
    

    新版Maven依赖如下

    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
    
    

    2. 配置swagger配置类

    1、Swagger实例Bean是Docket,所以通过配置Docket实例来配置Swaggger。

    最主要的还是点进Docket的源码吧!

    	@EnableSwagger2                // Swagger的开关,表示已经启用Swagger
        @Configuration                 // 声明当前配置类 注册到spring容器
        public class SwaggerConfiguration {
    
           @Value("${swagger.basePackage}")    
           private String basePackage;       // controller接口所在的包
    
           @Value("${swagger.title}")    
           private String title;           // 当前文档的标题
    
           @Value("${swagger.description}")
           private String description;         // 当前文档的详细描述
    
           @Value("${swagger.version}")
           private String version;   // 当前文档的版本
            
            
    
           @Bean //配置docket以配置Swagger具体参数
           public Docket createRestApi() {
              return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo2())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage(basePackage)) // 扫描包
                    .paths(PathSelectors.any())  // 路径
                    .build();
           }
    
        public ApiInfo apiInfo(){   // ApiInfo这个类没有set方法,只能构造全部属性,全部属性都要写很麻烦
            return   new ApiInfo("Title",
                    "Description",
                    "Version1.0",
                    "https://91mjw.com/",
                    new Contact("hujesse","https://www.baidu.com/","hujesse4@163.com"), //作者信息
                    "Apache 2.0License",
                    "http://www.apache.org/licenses/LICENSE-2.0 LicenseUrl",
                    new ArrayList<VendorExtension>());
        }
        // 可以使用ApiInfoBuilder 来选择实例化
        public ApiInfo apiInfo2(){  // 链式编程,看源码吧!
            return new ApiInfoBuilder()
                    .title("Title")
                    .description("Description")
                    .build();
        }
    
        }
    

    重点:现在有二个配置环境,如何让swagger在生成环境中使用而不是开发环境

    @Value("${swagger.flag}")
    private Boolean swaggerFlag;
        @Bean
        public Docket docket(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo2())
                    .enable(swaggerFlag)
                    .select()
                    // 扫描那个包下的注解
                    .apis(RequestHandlerSelectors.basePackage("com.hujesse.controller"))
                    .paths(PathSelectors.any())
                    .build(); //配置了swagger
        }
    

    3. application.yml添加配置

    #yaml配置swagger
            swagger:
                basePackage: com.hujesse.controller
                title: Title
                description: Des
                version: V1.0
                flag: true
    

    4. 添加接口

        @Api(value = "hello")
        @RestController
        public class TestController{
    
            @ApiOperation(value="byebye")
            @GetMapping("/hello")
            public String hello() {
                return "HelloSwa。";
            }
        }
    

    5. 美化UI

    <!-- 引入swagger-ui-layer包 /docs.html-->
    <dependency>
       <groupId>com.github.caspar-chen</groupId>
       <artifactId>swagger-ui-layer</artifactId>
       <version>1.1.3</version>
    </dependency>
    

    链接:https://www.bilibili.com/video/BV1PE411i7CV?p=50&spm_id_from=pageDriver

    狂神说java 47 - 50

    微信公众号Springboot14:集成swagger终极版

  • 相关阅读:
    composer阿里云短信服务不支持传参为数值--为2017年短信接口,2018阿里云有更新http://www.cnblogs.com/q1104460935/p/8916096.html
    随机生成字符串,数字,手机号,邮箱
    C#: .net序列化及反序列化 [XmlElement(“节点名称”)] [XmlAttribute(“节点属性”)] (上篇)
    自动升级功能
    C# WinForm 设置按纽为透明,使用背景色
    sql server 2000 单主键高效分页存储过程 (支持多字段排序)
    分页存储过程
    C# WinForm 解决子窗体放大后,子窗体图标放大的问题
    Windows 7/8 64位系统 不能注册32位dll 文件的解决方案
    添加ico图标
  • 原文地址:https://www.cnblogs.com/hujesse4/p/14811421.html
Copyright © 2011-2022 走看看