最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用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)在不同的迭代版本僅僅需要暴露該迭代版本的接口給外部使用,客戶(hù)端不需要關(guān)心不相關(guān)的接口

先來(lái)看張效果圖

下面是實(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è)用于版本常量的類(lèi)ApiVersionConstant

/**
 * api版本號(hào)常量類(lèi)
 * @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 接口測(cè)試")
                .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)前頁(yè)數(shù)", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "每頁(yè)大小", 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("分頁(yè)獲取資料文檔(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測(cè)試接口

swagger:自動(dòng)掃描 controller 包下的請(qǐng)求,生成接口文檔,并提供測(cè)試功能。

引入依賴(lài)

       <!-- 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 自定義配置類(lèi)

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")
                //簡(jiǎn)介
                .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

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

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

相關(guān)文章

  • mybatis學(xué)習(xí)筆記之mybatis注解配置詳解

    mybatis學(xué)習(xí)筆記之mybatis注解配置詳解

    本篇文章主要介紹了mybatis學(xué)習(xí)筆記之mybatis注解配置詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • 詳解Java實(shí)現(xiàn)簡(jiǎn)單SPI流程

    詳解Java實(shí)現(xiàn)簡(jiǎn)單SPI流程

    這篇文章主要介紹了Java實(shí)現(xiàn)簡(jiǎn)單SPI流程,SPI英文全稱(chēng)為Service Provider Interface,顧名思義,服務(wù)提供者接口,它是jdk提供給“服務(wù)提供廠商”或者“插件開(kāi)發(fā)者”使用的接口
    2023-03-03
  • Java 爬蟲(chóng)如何爬取需要登錄的網(wǎng)站

    Java 爬蟲(chóng)如何爬取需要登錄的網(wǎng)站

    這篇文章主要介紹了Java 爬蟲(chóng)如何爬取需要登錄的網(wǎng)站,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • springboot泛型封裝開(kāi)發(fā)方式

    springboot泛型封裝開(kāi)發(fā)方式

    這篇文章主要介紹了springboot泛型封裝開(kāi)發(fā)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 玩轉(zhuǎn)spring boot 結(jié)合jQuery和AngularJs(3)

    玩轉(zhuǎn)spring boot 結(jié)合jQuery和AngularJs(3)

    玩轉(zhuǎn)spring boot,這篇文章主要介紹了結(jié)合jQuery和AngularJs,玩轉(zhuǎn)spring boot,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Java簡(jiǎn)明解讀代碼塊的應(yīng)用

    Java簡(jiǎn)明解讀代碼塊的應(yīng)用

    所謂代碼塊是指用"{}"括起來(lái)的一段代碼,根據(jù)其位置和聲明的不同,可以分為普通代碼塊、構(gòu)造塊、靜態(tài)塊、和同步代碼塊。如果在代碼塊前加上 synchronized關(guān)鍵字,則此代碼塊就成為同步代碼塊
    2022-07-07
  • 重新理解Java泛型

    重新理解Java泛型

    這篇文章主要介紹了重新理解Java泛型,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • springboot配置開(kāi)發(fā)和測(cè)試環(huán)境并添加啟動(dòng)路徑方式

    springboot配置開(kāi)發(fā)和測(cè)試環(huán)境并添加啟動(dòng)路徑方式

    這篇文章主要介紹了springboot配置開(kāi)發(fā)和測(cè)試環(huán)境并添加啟動(dòng)路徑方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • JAVA面試題 從源碼角度分析StringBuffer和StringBuilder的區(qū)別

    JAVA面試題 從源碼角度分析StringBuffer和StringBuilder的區(qū)別

    這篇文章主要介紹了JAVA面試題 從源碼角度分析StringBuffer和StringBuilder的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面我們來(lái)一起學(xué)習(xí)下吧
    2019-07-07
  • Java中Stream?API的使用示例詳解

    Java中Stream?API的使用示例詳解

    Java?在?Java?8?中提供了一個(gè)新的附加包,稱(chēng)為?java.util.stream,該包由類(lèi)、接口和枚舉組成,允許對(duì)元素進(jìn)行函數(shù)式操作,?本文主要介紹了Java中Stream?API的具體使用,感興趣的小伙伴可以了解下
    2023-11-11

最新評(píng)論

舒城县| 绥芬河市| 上杭县| 东平县| 确山县| 育儿| 改则县| 甘谷县| 镇平县| 临沧市| 新安县| 宜川县| 凭祥市| 拜泉县| 安西县| 阿勒泰市| 纳雍县| 济源市| 永新县| 治多县| 奉化市| 珠海市| 大荔县| 嘉鱼县| 岳普湖县| 新余市| 尉犁县| 迁安市| 平阳县| 志丹县| 社旗县| 基隆市| 原阳县| 丹江口市| 霍林郭勒市| 华宁县| 林周县| 秦安县| 甘南县| 宁远县| 大余县|