集成步骤:

1、集成swagger需要导入下面两个依赖:

<!--引入swagger支持-->
<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>

2、还需要Swagger配置类

package cn.itsource.hrm.config;

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

/**
 * @ClassName: Swagger2
 * @description: 系统API接口文档配置类
 * @create: 2021-08-25 14:06:10
 * @Version 1.1.0.1
 */
@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //对外暴露服务的包,以controller的方式暴露,所以就是controller的包.
                .apis(RequestHandlerSelectors.basePackage("cn.itsource.hrm.web.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("系统服务API")
                .description("平台服务接口文档说明")
                .contact(new Contact("colin", "", "taotao@itsource.cn"))
                .version("1.0")
                .build();
    }

}

3、测试:http://localhost:1040/swagger-ui.html,可以访问这个地址查看是否集成成功。

网关配置Swagger

在zuul模块导入依赖:

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

创建两个配置类:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("人力资源系统")
                .description("人力资源接口文档说明")
                .termsOfServiceUrl("http://localhost:1020")
                .contact(new Contact("colin", "", "taotao@itsoruce.cn"))
                .version("1.0")
                .build();
    }

}

第二个:

@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {

    @Override
    public List<SwaggerResource> get() {
        List resources = new ArrayList<>();
        //注意:/hrm/system在yml配置文件已经配置了
        resources.add(swaggerResource("系统管理", "/hrm/system/v2/api-docs", "2.0"));
        resources.add(swaggerResource("课程管理", "/hrm/system/v2/api-docs", "2.0"));//测试一下
        return resources;
    }

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }
}

这里每次新增一个模块:复制一句修改这句代码即可: resources.add(swaggerResource("系统管理", "/hrm/system/v2/api-docs", "2.0"));

测试

浏览器输入:http://localhost:1020/swagger-ui.html(注意,这里访问的是Zuul服务)

看到如下说明集成成功

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐