美文网首页SpringBoot
springboot整合swagger

springboot整合swagger

作者: 东方舵手 | 来源:发表于2018-07-25 21:58 被阅读336次

    1. pom.xml文件中引入依赖

    <!-- swagger -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.7.0</version>
    </dependency>
    

    2. 创建swagger配置类

    @Configuration
    @EnableSwagger2
    public class SwaggerCofing {
    
        @Bean
        public Docket apiConfig(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()//创建ApiSelectorBuilder对象
                    .paths(Predicates.or(PathSelectors.regex("/api2/.*"))).build()//过滤的接口
                    .groupName("myapi") //定义分组
                    .apiInfo(apiInfo())// 调用apiInfo方法,创建一个ApiInfo实例,里面是展示在文档页面信息内容
                    .useDefaultResponseMessages(false)//关闭默认返回值
                    ;
        }
    
        @Bean
        public Docket wap_api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select().apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.ant("/web/**")).build()//选定api的路径
                    .groupName("WEB接口文档V4.4").pathMapping("/")//创建第二个分组
                    .apiInfo(apiInfo2());
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("BookStore Platform API")//大标题
                    .description("BookStore Platform's REST API, all the applications could access the Object model data via JSON.")//详细描述
                    .version("2.0")//版本
                    .contact(new Contact("Helen", "http://qfedu.com", "123456@qq.com"))//作者
                    .license("The Apache License, Version 2.0")//许可证信息
                    .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")//许可证地址
                    .build();
        }
    
    }
    

    启动项目 访问ui地址

    http://localhost:8081/swagger-ui.html#/

    swagger界面.png

    相关文章

      网友评论

        本文标题:springboot整合swagger

        本文链接:https://www.haomeiwen.com/subject/utvymftx.html