接口测试工具

发布于:2022-12-04 ⋅ 阅读:(405) ⋅ 点赞:(0)

1.postman

Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件。postman被500万开发者和超100,000家公司用于每月访问1.3亿个API。java开发通常是作为后台开发语言,通常的项目中的接口开发需要一款测试工具来调试接口,这样无需前端页面也不耽误后台代码的开发进度,postman作为一个接口测试工具,是一个非常不错的选择。

官方网址:Postman

使用方法:

安装后即可使用

 

2.Swagger

Swagger 是一个规范完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务(API Documentation & Design Tools for Teams | Swagger)。 它的主要作用是:

  1. 使得前后端分离开发更加方便,有利于团队协作

  2. 接口的文档在线自动生成,降低后端开发人员编写接口文档的负担

  3. 功能测试

使用方法: SpringBoot集成Swagger

  1. 引入依赖,在主模块中引入该依赖
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
    </dependency>
  2. 在主工程的swagger包中添加一个配置类
    @Configuration //控制类
    @EnableSwagger2   // 开启Swagger功能
    public class SwaggerConfiguration {
    
       @Bean
       public Docket buildDocket() {
          return new Docket(DocumentationType.SWAGGER_2)
                  .apiInfo(buildApiInfo())
                  .select()
                  // 要扫描的API(Controller)基础包
                  .apis(RequestHandlerSelectors.basePackage("com.heima"))
                  .paths(PathSelectors.any())
                  .build();
       }
    
       /**
        * 构建Api信息
        * @return
        */
       private ApiInfo buildApiInfo() {
          Contact contact = new Contact("黑马程序员","","");
          return new ApiInfoBuilder()
                  .title("黑马头条-平台管理API文档")
                  .description("平台管理服务api")
                  .contact(contact)
                  .version("1.0.0").build();
       }
    }
    

  3. 在子模块中添加配置类
    @Configuration
    @ComponentScan("com.heima.common.swagger")
    public class SwaggerConfig {
    }
  4. 在Controller(子模块控制层)中添加Swagger注解
    @RestController
    @RequestMapping("/api/v1/channel")
    @Api(tags = "频道管理API")
    public class WmChannelController {
    
        // 注入服务接口
        @Autowired
        private IWmChannelService wMChannelService;
    
        /**
         * 根据名称模糊查询分页列表
         *
         * @param dto
         * @return
         */
        @PostMapping("/list")
        @ApiOperation(value = "根据名称模糊查询分页列表", notes = "author:syl")  // value指名称  notes 备注
        @ApiImplicitParam(name = "dto", value = "查询对象", required = true, dataType = "ChannelDto")
        public ResponseResult listByName(@RequestBody ChannelDto dto) {
            return wMChannelService.listByName(dto);
        }
    }

  5. Dto中添加注解@ApiModelProperty
    @Data
    @EqualsAndHashCode(callSuper = true)
    public class ChannelDto extends PageRequestDto {
    
        /**
         * 频道名称
         */
        @ApiModelProperty(value = "频道名称")
        private String name;
    }
  6. 启动子模块微服务,访问地址:http://localhost:9001/swagger-ui.html

 

3.knife 4j

knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案,前身是swagger-bootstrap-ui,取名knife4j是希望它能像一把匕首一样小巧,轻量,并且功能强悍!

gitee地址:knife4j: Knife4j是一个集Swagger2 和 OpenAPI3为一体的增强解决方案

官方文档:Knife4j · 集Swagger2及OpenAPI3为一体的增强解决方案. | Knife4j

效果演示:http://knife4j.xiaominfo.com/doc.html

  1. 在主模块中的pom.xml文件中引入knife4j的依赖
    <dependency>
         <groupId>com.github.xiaoymin</groupId>
         <artifactId>knife4j-spring-boot-starter</artifactId>
    </dependency>

  2. 创建Swagger配置文件
    @Configuration
    @EnableSwagger2
    @EnableKnife4j
    @Import(BeanValidatorPluginsConfiguration.class)
    public class MyKnife4jConfiguration {
    
        @Bean(value = "defaultApi2")
        public Docket defaultApi2() {
            Docket docket=new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    //这里指定Controller扫描包路径
                    .apis(RequestHandlerSelectors.basePackage("com.heima"))
                    .paths(PathSelectors.any())
                    .build();
            return docket;
        }
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("黑马头条API文档")
                    .description("黑马头条API文档")
                    .version("1.0")
                    .build();
        }
    }
    
    

  3. 在子模块控制类中开启配置
    @Configuration
    // @ComponentScan("com.heima.common.swagger")
    @ComponentScan("com.heima.common.knife4j")
    public class SwaggerConfig {
    }

  4. 在浏览器输入地址:http://host:port/doc.html

 

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到