欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Swagger實(shí)現(xiàn)接口版本號(hào)管理方式

 更新時(shí)間:2021年10月13日 12:01:37   作者:被迫成為奮斗b  
這篇文章主要介紹了使用Swagger實(shí)現(xiàn)接口版本號(hào)管理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Swagger實(shí)現(xiàn)接口版本號(hào)管理

前言:使用swagger暴露對(duì)外接口時(shí)原則是每個(gè)系統(tǒng)在不同的迭代版本僅僅需要暴露該迭代版本的接口給外部使用,客戶端不需要關(guān)心不相關(guān)的接口

先來看張效果圖

下面是實(shí)現(xiàn)代碼:

定義注解ApiVersion:

/**
 * 接口版本管理注解
 * @author 周寧
 * @Date 2018-08-30 11:48
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ApiVersion {
 
    /**
     * 接口版本號(hào)(對(duì)應(yīng)swagger中的group)
     * @return String[]
     */
    String[] group();
 
}

定義一個(gè)用于版本常量的類ApiVersionConstant

/**
 * api版本號(hào)常量類
 * @author 周寧
 * @Date 2018-08-30 13:30
 */
public interface ApiVersionConstant {
    /**
     * 圖審系統(tǒng)手機(jī)app1.0.0版本
     */
    String FAP_APP100 = "app1.0.0";
 
}

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

@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {
    
    //默認(rèn)版本的接口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版本對(duì)外接口
    @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();
    } 
}

立即食用

/**
 * @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 = "當(dāng)前頁數(shù)", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "每頁大小", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "projectId", value = "項(xiàng)目id", dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "stageNum", value = "階段編號(hào)", dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "0資料(文檔);1cad圖紙", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "searchkey", value = "搜索關(guān)鍵字", dataType = "string", paramType = "query")})
    @ApiOperation("分頁獲取資料文檔(CAD圖紙)列表數(shù)據(jù)")
    @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:自動(dòng)掃描 controller 包下的請(qǐng)求,生成接口文檔,并提供測試功能。

引入依賴

       <!-- 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 自定義配置類

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()
                //標(biāo)題
                .title("Spring Boot 中使用 Swagger2 構(gòu)建 RESTful APIs")
                //簡介
                .description("hello swagger")
                //服務(wù)條款
                .termsOfServiceUrl("1. xxx\n2. xxx\n3. xxx")
                //作者個(gè)人信息
                .contact(new Contact("admin", "http://www.zhiyou100.com", "admin@zhiyou100.com"))
                //版本
                .version("1.0")
                .build();
    }
}

啟動(dòng)項(xiàng)目后,使用 http://localhost:8080/swagger-ui.html

選擇需要測試的接口:Try it out -> 填寫參數(shù) -> Execute -> 查看響應(yīng)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot @PropertySource與@ImportResource有什么區(qū)別

    SpringBoot @PropertySource與@ImportResource有什么區(qū)別

    這篇文章主要介紹了SpringBoot @PropertySource與@ImportResource有什么區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-01-01
  • maven工程打包引入本地jar包的實(shí)現(xiàn)

    maven工程打包引入本地jar包的實(shí)現(xiàn)

    我們需要將jar包發(fā)布到一些指定的第三方Maven倉庫,本文主要介紹了maven工程打包引入本地jar包的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • 詳解Java實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)之并查集

    詳解Java實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)之并查集

    并查集這種數(shù)據(jù)結(jié)構(gòu),可能出現(xiàn)的頻率不是那么高,但是還會(huì)經(jīng)常性的見到,其理解學(xué)習(xí)起來非常容易,通過本文,一定能夠輕輕松松搞定并查集
    2021-06-06
  • elasticsearch kibana簡單查詢講解

    elasticsearch kibana簡單查詢講解

    今天小編就為大家分享一篇關(guān)于elasticsearch kibana簡單查詢講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Springboot+Flowable?快速實(shí)現(xiàn)工作流的開發(fā)流程

    Springboot+Flowable?快速實(shí)現(xiàn)工作流的開發(fā)流程

    這篇文章主要介紹了Springboot+Flowable?快速實(shí)現(xiàn)工作流的開發(fā)流程,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • spring jpa ManyToMany原理及用法詳解

    spring jpa ManyToMany原理及用法詳解

    這篇文章主要介紹了spring jpa ManyToMany原理及用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • 簡單了解java獲取類的3種方式

    簡單了解java獲取類的3種方式

    這篇文章主要介紹了java獲取類的3種方式詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Springboot傳參詳解

    Springboot傳參詳解

    這篇文章主要介紹了Springboot傳參的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-11-11
  • 23種設(shè)計(jì)模式(8) java外觀模式

    23種設(shè)計(jì)模式(8) java外觀模式

    這篇文章主要為大家詳細(xì)介紹了23種設(shè)計(jì)模式之java外觀模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • java實(shí)現(xiàn)簡易版簡易版dubbo

    java實(shí)現(xiàn)簡易版簡易版dubbo

    dubbo是阿里開源的rpc框架,目前是apache頂級(jí)開源項(xiàng)目,可以用來構(gòu)建微服務(wù)。本文主要介紹了如何通過java實(shí)現(xiàn)簡易版的dubbo,感興趣的小伙伴可以了解一下
    2021-11-11

最新評(píng)論