• SpringBoot整合Swagger


    SpringBoot整合Swagger

    1.首先创建一个springboot项目

    2.引入依赖

    <!--swagger-->
            <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.编写Swagger配置类

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.niuben.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("测试Swagger")
                    .description("这是一个Restful风格的API文档")
                    .version("1.0")
                    //.contact(new Contact("负责人", "http://xxx.xxx.com/负责人访问链接", "负责人邮箱"))
                    .build();
        }
    }

    4.启动项目

    对Swagger配置类的详解

    配置扫描接口

     通过 .select() 方法,去配置扫描接口, RequestHandlerSelectors.xx 配置如何扫描接口

    any():扫描所有,项目中的所有接口都会被扫描到
    none():不扫描接口
    basePackage(final String basePackage):根据包路径扫描接口
    withMethodAnnotation(final Class<? extends Annotation> annotation):通过方法上的注解扫描,如withMethodAnnotation(GetMapping.class)只扫描get请求
    withClassAnnotation(final Class<? extends Annotation> annotation):通过类上的注解扫描,如.withClassAnnotation(Controller.class)只扫描有controller注解的类中的接口

    配置接口扫描过滤

    any() :任何请求都扫描
    none() :任何请求都不扫描
    regex(final String pathRegex) :通过正则表达式控制
    ant(final String antPattern) :通过ant()控制

    动态配置根据环境时显示swagger

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean
        public Docket createRestApi(Environment environment) {
            // 设置要test、dev环境时显示swagger,处于prod时不显示
            Profiles of = Profiles.of("dev", "test");
            // 判断当前是否处于该环境
            boolean b = environment.acceptsProfiles(of);
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                    .enable(b) // 通过 enable() 接收此参数判断是否要显示
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.niuben.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("测试Swagger")
                    .description("这是一个Restful风格的API文档")
                    .version("1.0")
                    //.contact(new Contact("负责人", "http://xxx.xxx.com/负责人访问链接", "负责人邮箱"))
                    .build();
        }
    }
  • 相关阅读:
    在次转发IBATIS模糊查询
    IBatisNet 模糊查询
    转js动态给table添加行(tr)
    简单的ajax(菜鸟级)
    Redis消息通知系统的实现 新风宇宙
    彻底删除SVN版本库某一文件夹或文件 新风宇宙
    代码审查:ThoughtBot官方给出的代码审查指导原则 新风宇宙
    linux下php代码加密扩展beast 新风宇宙
    利用curl的API进行开发 新风宇宙
    Linux 日志服务器搭建(rsyslog+loganalyzer) 新风宇宙
  • 原文地址:https://www.cnblogs.com/niudaben/p/11882330.html
Copyright © 2020-2023  润新知