zoukankan      html  css  js  c++  java
  • 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  (记得修改端口)


  • 相关阅读:
    8 Django 模型层(1)
    7 Django的模板层
    Java ClassLoader
    Spring的注入注解
    Java 面试Spring的加载机制
    Spring容器启动初始化bean的方法
    java 线程
    经典博客
    Spring注解@Component、@Repository、@Service、@Controller区别
    java 实现多个文件的Zip包的生成
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/13261589.html
Copyright © 2011-2022 走看看