Swagger配置使用

本文介绍了如何在项目中配置和使用Swagger,包括添加Maven依赖,创建configuration对象,以及在model层和controller层的应用。在完成配置后,可以通过访问http://localhost:8095/swagger-ui.html来查看和测试API。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Maven包

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox-swagger2.version}</version>
</dependency>
<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox-swagger-ui.version}</version>
 </dependency>

创建configuation对象

package com.project.automationplatform.configuration;


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;

/*
  配置Swagger2对象, 配合controller和model中的注解, 接口和对应参数信息会自动生成, 无需手动添加
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    //api接口包扫描路径
    public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.project.automationplatform.controller";

    public static final String VERSION = "1.0.0";

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                //用于分组功能,也可以不配置
                .groupName("automation-platform")
                //注册整体api信息
                .apiInfo(apiInfo())
                //swagger功能是否启用,可以通过配置设置,也可以先写死
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("自动化测试平台")
                .description("自动化测试平台API接口文档")
                .contact(new Contact("KevinZhu", "", "zhukaiyu@jd.com"))
                .version(VERSION)
                .build();
    }

}

使用

  1. model层
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/*
swagger注解
@ApiModel(value="测试用例表", description="自动化测试用例步骤详情储存表")   指定整个实体的基本信息
@ApiModelProperty(value = "测试用例编号", required=true)    指定每个参数swagger中显示的名字和基本属性
 */
//除上述2个注解, 其他是lombok注解

@Data
@TableName(value = "testcase_automation")    //第一次使用,将表名改为自己应用数据库中创建的表名
@ApiModel(value="测试用例表", description="自动化测试用例步骤详情储存表")
@ToString(callSuper=true, includeFieldNames=true)
@AllArgsConstructor
@NoArgsConstructor
public class TestCaseTemplate implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "测试用例属于的应用")
    @TableField(value = "test_case_app")
    private String testCaseApp;

    @ApiModelProperty(value = "测试用例编号", required=true)
    @TableId(value = "test_case_id")
    private String testCaseID;
}
  1. controller层
@Api(value = "自动化测试平台API接口")
@RestController
@RequestMapping("/api/testCase")
public class TestCaseTemplateController {

	@ApiOperation(value = "通过TestCaseId获取测试用例详情")
    @RequestMapping(value = "/querySteps",method= RequestMethod.GET)
    public ResponseResult getTestCaseInfo(@RequestParam(value="testCaseID") String testCaseID) {
    }
  1. 启动应用后, 打开网页:
    http://localhost:8095/swagger-ui.html
    端口是应用端口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值