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

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

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

Swagger實現(xiàn)接口版本號管理

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

先來看張效果圖

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

定義注解ApiVersion:

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

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

/**
 * api版本號常量類
 * @author 周寧
 * @Date 2018-08-30 13:30
 */
public interface ApiVersionConstant {
    /**
     * 圖審系統(tǒng)手機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版本對外接口
    @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 = "項目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 = "搜索關(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:自動掃描 controller 包下的請求,生成接口文檔,并提供測試功能。

引入依賴

       <!-- 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")
                //作者個人信息
                .contact(new Contact("admin", "http://www.zhiyou100.com", "admin@zhiyou100.com"))
                //版本
                .version("1.0")
                .build();
    }
}

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

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

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

相關(guān)文章

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

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

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

    詳解Java實現(xiàn)簡單SPI流程

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

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

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

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

    這篇文章主要介紹了springboot泛型封裝開發(fā)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    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,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Java簡明解讀代碼塊的應(yīng)用

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

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

    重新理解Java泛型

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

    springboot配置開發(fā)和測試環(huán)境并添加啟動路徑方式

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

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

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

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

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

最新評論

闸北区| 东平县| 灌阳县| 额尔古纳市| 江都市| 绥德县| 洪雅县| 新密市| 巴中市| 富裕县| 新蔡县| 仁化县| 喀喇沁旗| 全南县| 高清| 玛多县| 溧水县| 清新县| 芜湖县| 盖州市| 宁津县| 柘城县| 松江区| 高雄县| 霍城县| 汝南县| 辉南县| 宁城县| 德昌县| 临澧县| 红安县| 青神县| 阳高县| 镇远县| 廉江市| 高清| 通山县| 张北县| 宁津县| 邮箱| 福安市|