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

如何使用Springfox?Swagger實現(xiàn)API自動生成單元測試

 更新時間:2024年04月11日 10:55:05   作者:W-琑  
Springfox是一個使用Java語言開發(fā)開源的API Doc的框架,它的前身是swagger-springmvc,可以將我們的Controller中的方法以文檔的形式展現(xiàn),這篇文章主要介紹了如何使用Springfox?Swagger實現(xiàn)API自動生成單元測試,感興趣的朋友跟隨小編一起看看吧

Springfox 簡介

Springfox 是一個使用Java語言開發(fā)開源的API Doc的框架, 它的前身是swagger-springmvc,可以將我們的Controller中的方法以文檔的形式展現(xiàn)。官方定義為: Automated JSON API documentation for API’s built with Spring。

Springfox 目前有1、2、3三種版本,從v3版本開始則有較大變化,據(jù)官方文檔介紹,變化如下:

刪除早期版本的一些依賴。特別是刪除springfox-swagger2和springfox-swagger-ui依賴。

  • 刪除 @EnableSwagger2 注釋
  • 添加 springfox-boot-starter 支持springboot使用的起步依賴
  • Springfox 3.x 刪除了對 guava 和其他第三方庫的依賴(但仍然依賴于 spring 插件和開放 api 庫,用于注釋和模型)

Springfox 的作用

1)將前后端有效分離,并保證了API與文檔的實時同步
2)使用springfox生成的接口文檔直觀可視,支持查看各個接口需要的參數(shù)和返回結(jié)果
3)springfox支持在線測試,可實時檢查參數(shù)和返回值

接下來介紹如何使用Springfox Swagger實現(xiàn)API自動生成單元測試。

第一步:在pom.xml中添加依賴

        <!-- API?檔?成,基于swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- SpringBoot健康監(jiān)控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

第二步:加入以下代碼,并作出適當(dāng)修改

package com.bitejiuyeke.forum.config;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
 * Swagger配置類
 */
// 配置類
@Configuration
// 開啟Springfox-Swagger
@EnableOpenApi
public class SwaggerConfig {
    /**
     * Springfox-Swagger基本配置
     * @return
     */
    @Bean
    public Docket createApi() {
        Docket docket = new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.btjyk.forum.controller")) //根據(jù)自己controller包的 路徑自行修改
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    // 配置API基本信息
    private ApiInfo apiInfo() {//以下 基本信息均可修改
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("線上論壇系統(tǒng)API")
                .description("線上論壇系統(tǒng)前后端分離API測試")
                .contact(new Contact("Bit Tech",
                        "https://edu.btjyk.com", "1598374550@qq.com"))
                .version("1.0")
                .build();
        return apiInfo;
    }
    /**
     * 解決SpringBoot 6.0以上與Swagger 3.0.0 不兼容的問題
     * 復(fù)制即可
     **/
    @Bean
    public WebMvcEndpointHandlerMapping
    webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
                                     ServletEndpointsSupplier servletEndpointsSupplier,
                                     ControllerEndpointsSupplier controllerEndpointsSupplier,
                                     EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,
                                     WebEndpointProperties webEndpointProperties, Environment environment) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
        Collection<ExposableWebEndpoint> webEndpoints =
                webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping =
                this.shouldRegisterLinksMapping(webEndpointProperties, environment,
                        basePath);
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints,
                endpointMediaTypes,
                corsProperties.toCorsConfiguration(), new
                EndpointLinksResolver(allEndpoints, basePath),
                shouldRegisterLinksMapping, null);
    }
    private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
        return webEndpointProperties.getDiscovery().isEnabled()
                && (StringUtils.hasText(basePath)
                || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
    }
}

第三步:在application.yaml中添加

spring
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher #Springfox-Swagger兼容性配置

第四步:添加注解

  • @Api: 作?在Controller上,對控制器類的說明 。tags="說明該類的作?,可以在前臺界?上看到的注解"
  • @ApiModel: 作?在響應(yīng)的類上,對返回響應(yīng)數(shù)據(jù)的說明
  • @ApiModelProerty:作?在類的屬性上,對屬性的說明
  • @ApiOperation: 作?在具體?法上,對API接?的說明
  • @ApiParam:作?在?法中的每?個參數(shù)上,對參數(shù)的屬性進?說明

啟動程序,瀏覽器中輸?地址:http://127.0.0.1:端口號/swagger-ui/index.html ,可以正常并 顯?接?信息,說明配置成功,此時接?信息已經(jīng)顯?出來了,可以分別針對每個接?進?測試,具 體操作按??指引即可。

另外:還可以導(dǎo)出到postman

1.復(fù)制

2.打開postman

3.粘貼

4.點擊import即可

到此這篇關(guān)于如何使用Springfox Swagger實現(xiàn)API自動生成單元測試的文章就介紹到這了,更多相關(guān)Springfox Swagger 單元測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

黑龙江省| 鹤庆县| 潜山县| 惠来县| 称多县| 鄂州市| 宜良县| 黄陵县| 博乐市| 屯门区| 古浪县| 杭锦旗| 大田县| 北宁市| 长岭县| 中阳县| 成安县| 芮城县| 怀仁县| 日土县| 建始县| 蚌埠市| 长兴县| 二连浩特市| 化德县| 社会| 凯里市| 招远市| 鞍山市| 湄潭县| 崇仁县| 九龙县| 正蓝旗| 周口市| 嫩江县| 巧家县| 瓦房店市| 波密县| 本溪市| 阿坝县| 浑源县|