Springboot集成Swagger详解经过

Springboot集成Swagger以及配置Swagger

若看不懂,可点击此视频链接https://www.bilibili.com/video/BV1Y441197Lw

最终的依赖和代码

依赖

    <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>

最终代码 复制粘贴 运行项目 输入http://localhost:8080/swagger-ui.html 即可看到集成与配置完成

代码

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

 	//Swagger 需要Docket Bean 通过构造方法 具体需要看源码
	 @Bean
	 public Docket docket( ){
	 
   				 return new Docket(DocumentationType.SWAGGER_2)
            					.apiInfo(apiInfo())//配置文档信息
           						 .enable(true)//配置swagger是否启动
            //配置扫描.select().apis(RequestHandlerSelectors.basePackage("com.casaba.swagger.controller")).build();
            					.select()
           						.apis(RequestHandlerSelectors.basePackage("com.casaba.swagger.controller"))
           						.build();//工厂模式

	  }
   private ApiInfo apiInfo(){
   
    		Contact DEFAULT_CONTACT = new Contact("吴文霞","https://mp.csdn.net/console/article","1628794013@qq.com");
 			
 			return new ApiInfo("swagger接口文档"
				                , "开发新型"
				                , "1.0"
				                , "https://www.casaba.top/index.html"
				                , DEFAULT_CONTACT  //作者信息 Contact类
				                , "Apache 2.0"
				                , "http://www.apache.org/licenses/LICENSE-2.0"
				                , new ArrayList()
				                );
   }
}

配置经过分解

集成Swagger2

  1. maven导入依赖:百度maven 在maven repository 里搜 spring-box swagger2 找到 swagger 和 ui 两个依赖
    <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. 创建SwaggerConfig.java 类上加入@Configuration @EnableSwagger2 注解

    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    }
    
  2. 启动,打开http://localhost:8080/swagger-ui.html 即可看到页面

配置Swagger信息

  1. 创建构造方法 在构造方法上加上@Bean注解 返回Docket实体

    @Bean
    public Docket docket(&nbsp;){
    	return new Docket(DocumentationType.SWAGGER_2)
    					.apiInfo(apiInfo());//配置文档信息
     }
    
  2. 信息配置 new Docket(DocumentationType.SWAGGER_2).apiInfo(ApiInfo apiInfo) ApiInfo 需要参数,源码有,不细说

      private ApiInfo apiInfo( ){
     		Contact DEFAULT_CONTACT = new Contact("吴文霞","https://mp.csdn.net/console/article","1628794013@qq.com");
     		return new ApiInfo(
     		"swagger接口文档"
     		,"开发新型"
     		, "1.0"
     		, "https://www.casaba.top/index.html"
     		, DEFAULT_CONTACT  //作者信息 Contact类
     		, "Apache 2.0"
     		, "http://www.apache.org/licenses/LICENSE-2.0"
     		, new ArrayList());
     }
    

配置Swagger扫描接口Docket.select()

固定链式写法:

	 new Docket(DocumentationType.SWAGGER_2).select().apis(Predicate<RequestHandler>).build()
  • apis需要RequestHandler 而RequestHandlerSelectors继承了RequestHandler
    apis(RequestHandlerSelectors.basePackage(“com.casaba.swagger”))//指定扫描的包
    apis(RequestHandlerSelectors.any())//扫描全部
    apis(RequestHandlerSelectors.none())//全部不扫描
    apis(RequestHandlerSelectors.withClassAnnotation(RequestMapping.class)) //扫描类上有指定注解的
    apis(RequestHandlerSelectors.withMethodAnnotation(RequestMapping.class))//扫描方法上有指定注解的

配置swagger开关(不配置默认启用)

new Docket(DocumentationType.SWAGGER_2).enable(boolean)
//配置swagger是否启动 true启动 false关闭

进阶:无需.enable(boolean) 可将开关设置成开发环境启动,生产环境关闭接口文档
思路:
(1)在swagger2这个配置类上加@PropertySource("classpath:/config.properties"),@value获取值的方法(适合一套环境配置时使用)
(2)在配置类或配置方法上加@Profile("dev")//默认defule,若要这么做 需要你了解并会配置spring.profiles.active(适合多套环境配置时使用)

总结

学习过程中,所总结的注释在这里插入图片描述
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值