SpringBoot2.6.2及Swagger3使用


一、swagger的pom.xml引入

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

二、增加配置类

package cn.gzsendi.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @Description: Swagger3配置文件
 * http://localhost:18181/aiVideoSystem/swagger-ui/index.html
*/
@SpringBootConfiguration
@EnableOpenApi
public class Swagger3Config {
    
    
	
	private static final String PACKAGE = "cn.gzsendi";
	
    /**
     *   application中还配置了mvc,因为springboot2.6.2与swagger3不兼容
     */

    /**
     * ture 启用Swagger3.0, false 禁用(生产环境要禁用)
     */
    //是否开启swagger功能
  	@Value("${swagger.enabled:true}")
  	private boolean swaggerEnabled;
    
    @Bean
    public Docket createRestApi(){
    
    
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                // 是否开启
                .enable(swaggerEnabled)
                .select()
                // 扫描的路径使用@Api的controller
				.apis(RequestHandlerSelectors.basePackage(PACKAGE))
				// 配置了@ApiOperation的注解的才暴露给swagger
				//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
    
    
        return new ApiInfoBuilder()
                .title("Swagger3接口文档")
                .description("spring boot 系统管理接口文档")
                //作者信息
                .contact(new Contact("admin","https://test123456.com/", "[email protected]"))
                .version("1.0")
                .build();
    }
}

三、增加配置application.yml

spring:
	#swagger3 需配置,不然展示不了列表,这个mvc的配置是springboot2.6.2不支持swagger3的折衷配置,可考虑升级Springboot版本或降级版本
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
      
#是否开启swagger,不设置默认值为true,生产环境中建议设置成false
swagger: 
  enabled: true
  

四、测试

http://Ip:Port/swagger-ui/index.html

本文内容参考https://blog.csdn.net/yao22yao/article/details/125207679

猜你喜欢

转载自blog.csdn.net/jxlhljh/article/details/126629230