Swagger 微服務部署


一,pom添加

我使用的bootstrap-ui。你們可以使用springfox-swagger-ui

<!--    swagger    -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

 

二,注冊中心

在正式和測試,添加不同的配置。

dev:

#開啟swagger
swagger:
  enable: true

 

prod:

#開啟swagger
swagger:
  enable: false

 

三,添加配置類

在需要的微服務上,添加配置類

 

注意:@ConditionalOnProperty(name = "swagger.enable", havingValue = "true") 是讀取配置文件

package com.kps.webAPIPOExternal;

import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 *@author Tyler
 *@date 2020/6/24
 */

@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("API Document")
                        .description("API Document")
                        .version("1.0")
                        .build()
                )
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

}

 

 

四,啟動

啟動注冊中心,配置中心,服務

然后訪問:http://localhost:9300/doc.html  (記得修改端口)


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM