在微服务里集成swagger

一般在有controller层的模块集成swagger;废话不多说,直接看步骤。

1.导包

<!--引入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.主要起作用的类  Swagger2

@Configuration

@EnableSwagger2

public class Swagger2 {

    @Bean

    public Docket createRestApi() {

        return new Docket(DocumentationType.SWAGGER_2)

                .apiInfo(apiInfo())

                .select()

                //对外暴露服务的包,以controller的方式暴露,所以就是controller的包.

                .apis(RequestHandlerSelectors.basePackage("cn.zzq.system.web.controller"))

                .paths(PathSelectors.any())

                .build();

    }



    private ApiInfo apiInfo() {

        return new ApiInfoBuilder()

                .title("系统管理服务api")

                .description("系统管理服务接口文档说明")

                .contact(new Contact("zzq", "", "zzq@itsource.cn"))

                .version("1.0")

                .build();

    }

}

3.测试

项目地址:http://ip:端口/swagger-ui.html


在网关模块中配置

1.导包

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->

        <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.SwaggerResourcesProvider类

@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {


    //整合每个微服务的swagger

    @Override

    public List<SwaggerResource> get() {
        List resources = new ArrayList<>();
        //一个 SwaggerResource对应一个微服务 : 参数: 服务中文名 , 路径:/zuul前缀/服务的routes访问路径//v2/api-docs  ; 版本
        resources.add(swaggerResource("系统管理", "/hrm/system/v2/api-docs", "2.0"));
        resources.add(swaggerResource("课程管理", "/hrm/course/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;

    }

}











3.SwaggerConfig类

@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") //zuul的地址

                .contact(new Contact("zzq", "", "zzq@itsoruce.cn"))

                .version("1.0")

                .build();

    }

}

  关注公众号,获取免费软件、资料,笔记等。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心之所向...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值