springboot整合Swagger3.0

springboot整合Swagger3.0

  1. 导入依赖
<!-- swagger3依赖 -->
<properties>
        <java.version>1.8</java.version>
        <swagger.version>3.0.0</swagger.version>
        <swagger.models.version>1.6.2</swagger.models.version>
    </properties>
        
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>${swagger.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>${swagger.models.version}</version>
        </dependency>
  1. 配置文件
    注意注解的变化:

@EnableOpenApi

import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger接口配置
 *
 * @author Administrator
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {

    /**
     * 是否开启swagger
     */
    @Value("${swagger.enabled}")
    private boolean enabled;

    /**
     * 设置请求的统一前缀
     */
    @Value("${swagger.pathMapping}")
    private String pathMapping;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                //是否启用swagger
                .enable(enabled)
                //从来创建API的基本信息,展示在文档的页面中(自定义展示的信息)
                .apiInfo(apiInfo())
                //设置哪些接口暴露给swagger展示设置
                .select()
                //扫描所有注解的api,用这种方式更灵活
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //.apis(RequestHandlerSelectors.basePackage("com.gsl"))
                //扫描所有.apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        //用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                //设置标题
                .title("CMS系统")
                //描述
                .description("描述信息")
                //作者信息
                .contact(new Contact(null, null, null))
                //版本号
                .version("版本号:" + null)
                .build();
    }
}

  1. 控制器
import com.gsl.system.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/swagger")
@Api(tags = "swagger测试请求")
public class SwaggerController extends BaseController {

    @GetMapping
    @ApiOperation(value = "测试",notes = "测试")
    public String swaggerIndex() {
        System.out.println("请求swagger页面转发");
        return redirect("/swagger-ui.html");
    }
}
  1. yml配置文件
swagger:
  # 是否开启swagger
  enabled: true
  # swagger 请求前缀
  pathMapping: /dev-api

访问页面报错404,解决方法:https://blog.csdn.net/xyh930929/article/details/109537258

你可能感兴趣的:(Java,spring,boot,swagger3)