使用Springfox Swagger实现API自动生成单元测试

目录

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

第二步:加入以下代码,并作出适当修改

第三步:在application.yaml中添加

第四步:添加注解

第五步:运行成功之后,访问相应网址

另外:还可以导出到postman


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

        <!-- API⽂档⽣成,基于swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- SpringBoot健康监控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

第二步:加入以下代码,并作出适当修改

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")) //根据自己controller包的 路径自行修改
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    // 配置API基本信息
    private ApiInfo apiInfo() {//以下 基本信息均可修改
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("线上论坛系统API")
                .description("线上论坛系统前后端分离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 不兼容的问题
     * 复制即可
     **/
    @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: 作⽤在响应的类上,对返回响应数据的说明
  • @ApiModelProerty:作⽤在类的属性上,对属性的说明
  • @ApiOperation: 作⽤在具体⽅法上,对API接⼝的说明
  • @ApiParam:作⽤在⽅法中的每⼀个参数上,对参数的属性进⾏说明

第五步:运行成功之后,访问相应网址

启动程序,浏览器中输⼊地址:http://127.0.0.1:端口号/swagger-ui/index.html ,可以正常并 显⽰接⼝信息,说明配置成功,此时接⼝信息已经显⽰出来了,可以分别针对每个接⼝进⾏测试,具 体操作按⻚⾯指引即可。

另外:还可以导出到postman

1.复制

2.打开postman

3.粘贴

4.点击import即可

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值