Swagger(二)Springboot 整合 Swagger

SpringBoot集成Swagger

  1. 新建-个SpringBoot = web项目
  2. 导入相关依赖
<!-- 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>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
  1. 创建一个Swagger类
package com.example.demo1.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author MW
 * @Package
 * @Description: 本类用于
 * @date 2019/12/27 9:30
 */
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {

}
  1. 运行测试
    http://localhost:8080/swagger-ui.html#/
    在这里插入图片描述

配置Swagger信息

 private ApiInfo apiInfo(){
        //作者信息
        Contact DEFAULT_CONTACT = new Contact("MW", "http://localhost:8080/", "[email protected]");
        return new ApiInfo(
                "配置Swagger",
                "练习配置Swagger",
                "1.0",
                "urn:tos",
                DEFAULT_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).groupName("MW")
                .apiInfo(apiInfo())
                //enable :是否启动swagger,false不启动,true启动
                // .enable(false)
                .select()
                /*RequestHandlerSelectors :配置要扫描接口的方式
                 * basePackage() :扫描指定的包
                 * any() : 扫描全部
                 * none() : 不扫描
                 * withClassAnnotation() :扫描类上的注解,参数是一个注解的对象
                 * withMethodAnnotation() :扫描方法上的注解
                 */
                .apis(RequestHandlerSelectors.basePackage("com.example.demo1.controller"))
                //paths :过滤什么路径
//                .paths(PathSelectors.ant("com.example.demo1.controller/*"))
                .build()
                ;
    }

实现分组;
.groupName(“mw”)

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

  @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("小");
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("可");
    }

    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("爱");
    }
发布了45 篇原创文章 · 获赞 7 · 访问量 923

猜你喜欢

转载自blog.csdn.net/qq_42222342/article/details/103838612
今日推荐