一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 使用Swagger實現接口版本號管理方式

使用Swagger實現接口版本號管理方式

2022-02-17 15:32被迫成為奮斗b Java教程

這篇文章主要介紹了使用Swagger實現接口版本號管理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Swagger實現接口版本號管理

前言:使用swagger暴露對外接口時原則是每個系統在不同的迭代版本僅僅需要暴露該迭代版本的接口給外部使用,客戶端不需要關心不相關的接口

先來看張效果圖

使用Swagger實現接口版本號管理方式

下面是實現代碼:

定義注解ApiVersion:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * 接口版本管理注解
 * @author 周寧
 * @Date 2018-08-30 11:48
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ApiVersion {
 
    /**
     * 接口版本號(對應swagger中的group)
     * @return String[]
     */
    String[] group();
 
}

定義一個用于版本常量的類ApiVersionConstant

?
1
2
3
4
5
6
7
8
9
10
11
12
/**
 * api版本號常量類
 * @author 周寧
 * @Date 2018-08-30 13:30
 */
public interface ApiVersionConstant {
    /**
     * 圖審系統手機app1.0.0版本
     */
    String FAP_APP100 = "app1.0.0";
 
}

更改SwaggerConfig添加Docket(可以理解成一組swagger 接口的集合),并定義groupName,根據ApiVersion的group方法區分不同組(迭代)的接口,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {
    
    //默認版本的接口api-docs分組
    @Bean
    public Docket vDefault(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gysoft"))//controller路徑
                .paths(PathSelectors.any())
                .build();
    }
    //app1.0.0版本對外接口
    @Bean
    public Docket vApp100(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .groupName(FAP_APP100)
                .select()
                .apis(input -> {
                    ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
                    if(apiVersion!=null&&Arrays.asList(apiVersion.group()).contains(FAP_APP100)){
                        return true;
                    }
                    return false;
                })//controller路徑
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo buildApiInf(){
        return new ApiInfoBuilder()
                .title("接口列表")
                .termsOfServiceUrl("http://127.0.0.1:8080/swagger-ui.html")
                .description("springmvc swagger 接口測試")
                .version("1.0.0")
                .build();
    }
}

立即食用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * @author 周寧
 * @Date 2018-08-24 11:05
 */
@RestController
@RequestMapping("/document")
@Api(value = "資料文檔或者CAD圖紙", description = "資料文檔或者CAD圖紙")
public class DocumentController extends GyBasicSession {
 
    private static final Logger logger = LoggerFactory.getLogger(DocumentController.class);
 
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "當前頁數", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "每頁大小", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "projectId", value = "項目id", dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "stageNum", value = "階段編號", dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "0資料(文檔);1cad圖紙", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "searchkey", value = "搜索關鍵字", dataType = "string", paramType = "query")})
    @ApiOperation("分頁獲取資料文檔(CAD圖紙)列表數據")
    @GetMapping("/pageQueryAppDocumentInfo/{page}/{pageSize}/{projectId}/{stageNum}/{type}")
    @ApiVersion(group = ApiVersionConstant.FAP_APP100)
    public PageResult<AppDocumentInfo> pageQueryAppDocumentInfo(@PathVariable Integer page, @PathVariable Integer pageSize, @PathVariable String projectId, @PathVariable String stageNum, @PathVariable Integer type, @RequestParam String searchkey) {
        return null;
    }
}

使用swagger測試接口

swagger:自動掃描 controller 包下的請求,生成接口文檔,并提供測試功能。

引入依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
<!-- 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>

在 config 包引入 swagger 自定義配置類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.zhiyou100.zymusic.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;
/**
 * @author teacher
 * @date 2019/9/25
 */
@Configuration
@EnableSwagger2
public class MySwaggerConfiguration {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //標題
                .title("Spring Boot 中使用 Swagger2 構建 RESTful APIs")
                //簡介
                .description("hello swagger")
                //服務條款
                .termsOfServiceUrl("1. xxx\n2. xxx\n3. xxx")
                //作者個人信息
                .contact(new Contact("admin", "http://www.zhiyou100.com", "[email protected]"))
                //版本
                .version("1.0")
                .build();
    }
}

啟動項目后,使用 http://localhost:8080/swagger-ui.html

選擇需要測試的接口:Try it out -> 填寫參數 -> Execute -> 查看響應

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qq_23536449/article/details/89878029

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一卡二卡四卡免费 | 狠狠综合久久综合网站 | 农村美女沟厕嘘嘘被偷看 | 青青青手机在线视频 | 特级老女人淫片高清视频 | 久久免费看少妇高潮A片JA | 亚洲人成绝费网站色ww | 亚洲国产经典 | 色综合天天综合网国产人 | 日韩亚洲欧美综合一区二区三区 | 日韩成人在线影院 | 国产一区二区精品久久 | 4tube欧美高清 | 学校捏奶揉下面污文h | 欧美久久久久久久一区二区三区 | 福利视频一区二区牛牛 | 欧美a在线 | 3x免费高清视频 | 顶级欧美做受xxx000大乳 | 97超pen个人视频公开视频视 | 私人影院在线免费观看 | 麻豆资源 | 无人区乱码区1卡2卡三卡在线 | 成人精品一级毛片 | 波多野结衣中文丝袜字幕 | 日本色吧 | 欧美一区二区三 | 美女视频ww8888网网 | 91精品综合 | vomoulei成人舞蹈 | 特级毛片全部免费播放器 | 男女视频在线观看 | 99久久一区二区精品 | 色久网 | 亚洲精品网址 | 国产在线视频一区二区三区 | 2021日本三级理论影院 | 成在线人免费 | 金牛网155755水心论坛黄大父母 | 欧美一级特黄刺激大片视频 | 欧美午夜寂寞影院安卓列表 |