• spring-boot 1.X集成swagger


    1、引入POM依赖

    <properties>
        <swagger-annotations.version>1.5.13</swagger-annotations.version>
        <springfox-swagger.version>2.7.0</springfox-swagger.version>
    </properties>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-annotations</artifactId>
                <version>${swagger-annotations.version}</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${springfox-swagger.version}</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${springfox-swagger.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    2、装配swagger

     1 package com.tomato.boss.common.autoconfig;
     2 
     3 import java.time.LocalDateTime;
     4 import java.util.ArrayList;
     5 import java.util.List;
     6 
     7 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
     8 import org.springframework.boot.context.properties.ConfigurationProperties;
     9 import org.springframework.context.annotation.Bean;
    10 import org.springframework.context.annotation.Configuration;
    11 
    12 import lombok.Getter;
    13 import lombok.Setter;
    14 import springfox.documentation.builders.ApiInfoBuilder;
    15 import springfox.documentation.builders.ParameterBuilder;
    16 import springfox.documentation.builders.PathSelectors;
    17 import springfox.documentation.builders.RequestHandlerSelectors;
    18 import springfox.documentation.schema.ModelRef;
    19 import springfox.documentation.service.ApiInfo;
    20 import springfox.documentation.service.Parameter;
    21 import springfox.documentation.spi.DocumentationType;
    22 import springfox.documentation.spring.web.plugins.Docket;
    23 import springfox.documentation.swagger2.annotations.EnableSwagger2;
    24 
    25 @Configuration
    26 @EnableSwagger2
    27 @ConditionalOnProperty("swagger.conf.host")
    28 @ConfigurationProperties("swagger.conf")
    29 @Setter
    30 @Getter
    31 public class Swagger2AutoConfiguration {
    32     private String groupName;
    33     private String basePackage;
    34     private String title;
    35     private String host;
    36     private String desc;
    37     private String serviceUrl;
    38     private String version;
    39 
    40     @Bean
    41     public Docket createRestApi() {
    42         // 添加header头
    43         List<Parameter> pars = new ArrayList<Parameter>() {
    44             private static final long serialVersionUID = 1L;
    45             { add(new ParameterBuilder().name("gsid").description("全局会话ID(open服务不需要)").modelRef(new ModelRef("string")).parameterType("header").required(false).build()); }
    46         };  
    47         
    48         return new Docket(DocumentationType.SWAGGER_2)
    49                 .groupName(groupName)
    50                 .apiInfo(apiInfo())
    51                 .host(host)
    52                 .select()
    53                 .apis(RequestHandlerSelectors.basePackage(basePackage))
    54                 .paths(PathSelectors.any())
    55                 .build()
    56                 .globalOperationParameters(pars);
    57     }
    58 
    59     private ApiInfo apiInfo() {
    60         return new ApiInfoBuilder()
    61                 .title(title)
    62                 .description(desc)
    63                 .termsOfServiceUrl(serviceUrl)
    64                 .version(version + LocalDateTime.now())
    65                 .build();
    66     }
    67 }

    3、properties配置

    swagger.conf.desc=api地址:http://${swagger.conf.host}/
    swagger.conf.service-url=http://${swagger.conf.host}/
    swagger.conf.title=BOSS项目组${swagger.conf.group-name}接口
    
    #注册中心列表直接跳转到swagger页面
    eureka.instance.status-page-url=http://${swagger.conf.host}/swagger-ui.html
    
    swagger.conf.host=bosstest.xx.com/api/sso
    swagger.conf.group-name=boss-sso-rest
    swagger.conf.base-package=com.tomato.boss.sso.rest
    swagger.conf.version=1.0

    当然,现在可以引入官方的start来自动注解

    <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.7.0.RELEASE</version>
    </dependency>

    当引入Zuul后可以在zuul层进行doc的集成,具体的实现方式可以参考:

    https://my.oschina.net/didispace/blog/1818417

  • 相关阅读:
    使用Node获取百度最新疫情信息,本地保存为json文件
    js实现将字符串首字母转换成大写
    egg实现登录鉴权(八):sequelize联表查询
    egg实现登录鉴权(七):权限管理
    egg实现登录鉴权(六):角色树的CRUD操作
    egg实现登录鉴权(五):mysql表中存储树形结构数据
    egg实现登录鉴权(四):人员新增和密码修改
    egg实现登录鉴权(三):密码的md5加密及验证
    egg实现登录鉴权(二):连接数据库(mysql)
    egg实现登录鉴权(一):生成token
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/10026120.html
Copyright © 2020-2023  润新知