swagger-ui2:3.0.0版本的使用

该版本发布日期为:
Jul, 2020
当前时间是最新 :2021年11月17日

2.2.92 版本有时会因为冲突找不到spring.mvc3.0的jar包, 所以升级为3.0

在线测试接口: http://localhost:9999/swagger-ui/index.html

1.pom中引用

     <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <swagger.version>3.0.0</swagger.version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <swagger.version>3.0.0</swagger.version>
        </dependency>

2.@EnableOpenApi配置

启动类和配置类都加上这个注解.

3.配置类

@Configuration
@EnableOpenApi
//@Profile({"dev","test"})
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                // 指定构建api文档的详细信息的方法:apiInfo()
                .apiInfo(apiInfo())
                .select( )
                // 指定要生成api接口的包路径,这里把controller作为包路径,生成controller中的所有接口
//                .apis(RequestHandlerSelectors.basePackage("com.cytx.financial.information.controller"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 构建api文档的详细信息
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 设置页面标题
                .title("标题")
                // 设置接口描述
                .description("description")
                // 设置联系方式
               // .contact("联系方式")
                // 设置版本
                .version("1.0")
                // 构建
                .build();
    }
}

本文由 hcb 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论