• Springboot集成Swagger2


    Swagger2简介

    1.随项目自动生成强大RESTful API文档,减少工作量
    2.API文档与代码整合在一起,便于同步更新API说明
    3.页面测试功能来调试每个RESTful API

    springboot集成Swagger2步骤

    1. 新建一个Springboot项目

    在这里插入图片描述

    2. 导入依赖

    在这里插入图片描述

    		  <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    		 <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.9.2</version>
            </dependency>
    

    3. 编写控制器

    在这里插入图片描述

    @RestController
    public class Mycontroller {
    
        @GetMapping({"/","hello"})
        public String hello(){
            return "helloword";
        }
    }
    

    4. 编写swagger的配置类

    写上注解
    @Configuration
    @EnableSwagger2
    在这里插入图片描述

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    }
    

    启动项目运行
    浏览器输入 http://localhost:8080/swagger-ui.html
    在这里插入图片描述
    我们所写的controller方法都会被检测到

    5. 配置Swagger2

    1. 配置Swagger信息 info
      在这里插入图片描述
        @Bean
        public Docket docket() {
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apinfo());
        }
        public ApiInfo apinfo(){
            Contact contact = new Contact(
                    "joker_dj",
                    "https://www.cnblogs.com/joker-dj/",
                    "2332810801@qq.com");
            return new ApiInfo("joker_dj的api文档",
                    "Api Documentation",
                    "1.0",
                    "https://www.cnblogs.com/joker-dj/",
                    contact, "Apache 2.0",
                    "http://www.apache.org/licenses/LICENSE-2.0",
                    new ArrayList());
        }
    

    运行看效果

    在这里插入图片描述
    2. 配置多个分组
    多配置几个Docket即可
    在这里插入图片描述
    在这里插入图片描述
    3. 配置其他的信息 不一一解释 都有注释
    在这里插入图片描述

    @Bean
        public Docket docket(){
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apinfo())
                    //是否启用swagger false不启用
                    .enable(true)
                    .groupName("joker_dj")
                    .select()
                    /*
                    配置RequestHandlerSelectors 扫描接口的方式
                    指定扫描的包basePackage
                    any()扫描全部
                    none()不扫描
                    withClassAnnotation:扫描类上的注解
                    */
                    .apis(RequestHandlerSelectors.basePackage("com.dj.controller"))
                    //过滤路径 比如PathSelectors.ant("login/**")
                   // .paths(PathSelectors.ant(""))
                    .build();
        }
    

    6.添加注释信息

    1. 编写user实体
      在这里插入图片描述
    2. 在controller返回一个user
      在这里插入图片描述
      运行
      在这里插入图片描述
      会检测到我们所有写的controller方法
      也可以进行测试方法
      点击Try it out
      在这里插入图片描述
    3. 点击运行
      在这里插入图片描述
      这里就会返回我们测试接口的信息以及响应的结果
      在这里插入图片描述
    4. 给类,属性和方法添加注释信息
      在user实体类添加注解
      @ApiModel:给类添加注释
      @ApiModelProperty :给字段添加注释
      在这里插入图片描述
    @ApiModel("用户实体类")
    public class user {
        @ApiModelProperty("用户名")
        public String username;
        @ApiModelProperty("密码")
        public String password;
    }
    

    运行看效果
    在这里插入图片描述
    添加到controller上
    @ApiOperation
    在这里插入图片描述
    运行看效果
    在这里插入图片描述
    5. 发送数据 测试接口
    在controller中编写代码
    在这里插入图片描述
    运行测试
    在这里插入图片描述
    输入用户名 密码 点击测试
    在这里插入图片描述
    在这里插入图片描述
    这样就返回了我们数据

    总结:Swagger这个工具主要是针对开发人员测试接口来使用的,
    注意:在项目上线的时候己得吧Swagger给关闭 防止接口暴露 (安全)

  • 相关阅读:
    视频编码之释——从H.261 到H.264
    bitmap图像介绍
    用搜索引擎搜索我的名字 @_@
    blog标题由来
    ORACLE双机热备安装及物理迁移 for win2000
    审核再次失败
    asp.net学习历程
    痛并快乐着
    开心,blog点击率超过1000
    XP下ASP.NET不能访问ORACLE数据库的解决方案
  • 原文地址:https://www.cnblogs.com/joker-dj/p/12693051.html
Copyright © 2020-2023  润新知