• Swagger学习笔记


    Swagger

    在项目中使用swagger需要springfox;

    • swagger2
    • ui

    SpringBoot继承Swagger

    1. 新建一个springboot==web项目

    2. 导入swagger依赖(在仓库搜索springfox swag)

      <!-- 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>
      
      1. 编写一个helloworld的程序

      2. 配置Swagger==》Config

        @Configuration
        @EnableSwagger2  //开启Swagger2
        public class SwaggerConfig {
        }
        
    3. 测试运行

      localhost:8080/swagger-ui.html

    配置Swagger

    Swagger的bean实例Docket

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket docket(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo());
        }
        //配置Swagger信息apiInfo
        private ApiInfo apiInfo(){
            //作者信息
             Contact contact = new Contact("黑黑", "www.baidu.com", "577055183@qq.com");
            return new ApiInfo(
                    "黑黑的Swagger日记",
                    "即使再小的帆也能远航",
                    "1.0",
                    "www.baidu.com",
                    contact,
                    "Apache 2.0",
                    "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
    
        }
    }
    

    Swagger配置扫描接口

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式
                //basePackage配置要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.lr.controller"))
                //过滤什么路径
                .paths(PathSelectors.ant("/kuang/**"))
                .build();
    }
    

    配置是否启动Swagger

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //禁用swagger
                .enable(false)
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式
                //basePackage配置要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.lr.controller"))
                //过滤什么路径
              //  .paths(PathSelectors.ant("/kuang/**"))
                .build();
    }
    

    我只希望我的Swagger在生产环境中使用,在发布的时候不适用?

    • 判断是不是生产环境 flag = false
    • 注入enable(flag)
    @Bean
    public Docket docket(Environment environment){
        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev","test");
        //获得项目的环境
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //禁用swagger
                .enable(flag)
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式
                //basePackage配置要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.lr.controller"))
                //过滤什么路径
              //  .paths(PathSelectors.ant("/kuang/**"))
                .build();
    }
    

    配置API文档的分组

    .groupName("黑黑")
    

    如何配置多个分组?多个Docket实即可

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket docket(Environment environment){
            //设置要显示的swagger环境
            Profiles profiles = Profiles.of("dev","test");
            //获得项目的环境
            boolean flag = environment.acceptsProfiles(profiles);
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .groupName("黑黑")
                    //禁用swagger
                    .enable(flag)
                    .select()
                    //RequestHandlerSelectors配置要扫描接口的方式
                    //basePackage配置要扫描的包
                    .apis(RequestHandlerSelectors.basePackage("com.lr.controller"))
                    //过滤什么路径
                  //  .paths(PathSelectors.ant("/kuang/**"))
                    .build();
        }
        @Bean
        public Docket docket1(Environment environment){
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("A");
        }
        @Bean
        public Docket docket2(Environment environment){
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("B");
        }
        @Bean
        public Docket docket3(Environment environment){
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("C");
        }
        //配置Swagger信息apiInfo
        private ApiInfo apiInfo(){
            //作者信息
             Contact contact = new Contact("黑黑", "www.baidu.com", "xxxxxxx@qq.com");
            return new ApiInfo(
                    "黑黑的Swagger日记",
                    "即使再小的帆也能远航",
                    "1.0",
                    "www.baidu.com",
                    contact,
                    "Apache 2.0",
                    "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
    
        }
    }
    

    实体类配置

    @ApiModel("用户实体类")
    public class User {
        @ApiModelProperty("用户名")
        public String username;
        @ApiModelProperty("密码")
        public String password;
    
    }
    
    @RestController
    public class HelloController {
        @GetMapping(value = "/hello")
        public String hello(){
            return "hello";
        }
        //
        @PostMapping(value = "/user")
        public User user(){
            return new User();
        }
        @ApiOperation("Hello控制类")
        @PostMapping ("/hello2")
        public String hello2(@ApiParam("用户名") String username){
                return "hello"+username;
        }
        @PostMapping ("/hello3")
        public User hello2(@ApiParam("用户名") User user){
            return user;
        }
    }
    

    总结:

    ​ 【注意点】:正式发布的时候关闭swagger!!!处于安全考虑。而且节省运行内存。

  • 相关阅读:
    How to show query plan and runtime statistic in Derby
    How to load Additional Supplied Modules for Postgres on Windows
    一步一步学List Control控件的用法(第一步)创建第一列
    如何为Postgresql数据库全文搜索(full text search)编写解析器(parser)
    [转]MFC和CLR的区别
    一步一步学Edit Control控件的用法
    将接收到的IP地址转化为字符串显示(代码)
    一步一步学List Control控件的用法(第三步)设置风格
    Andrew NgML第九&十章神经网络
    PAT 1019 General Palindromic Number[简单]
  • 原文地址:https://www.cnblogs.com/blackblack/p/13970240.html
Copyright © 2020-2023  润新知