SpringCloud学习笔记----swagger生成Api文档

179 阅读1分钟

使用简介

一般情况下 服务提供者模块会需要用到 具体在那个模块里面使用 需要自己定夺 如果是单纯SpringBoot项目就不用考虑了

maven引入依赖

    <!--swagger 相关依赖-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
    </dependency>

配置类

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                //只显示api路径下的页面
                //.paths(Predicates.and(PathSelectors.regex("/api/.*")))
                .build();
    }

    @Bean
    public Docket adminApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                //只显示admin路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .build();
    }

    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("网站-API文档")
                .description("本文档描述了网站微服务接口定义")
                .version("1.0")
                .contact(new Contact("atguigu", "http://atguigu.com", "493211102@qq.com"))
                .build();
    }

    private ApiInfo adminApiInfo(){
        return new ApiInfoBuilder()
                .title("后台管理系统-API文档")
                .description("本文档描述了后台管理系统微服务接口定义")
                .version("1.0")
                .contact(new Contact("atguigu", "http://atguigu.com", "49321112@qq.com"))
                .build();
    }
}

然后进入网址 会根据 controller 层自动生成文档

http://localhost:8201/swagger-ui.html 端口号是自己项目的端口号

image.png

相关注解的使用

//当前接口的注解 告诉别人这个接口是干啥用的
@ApiOperation(value = "医院设置:分页列表,不支持条件查询")

//这个是写在方法的形参括号里面  告诉别人此字段的用处
@ApiParam(name = "page",value = "当前页",required = true)