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)。 它的主要作用是:
使得前后端分离开发更加方便,有利于团队协作
接口的文档在线自动生成,降低后端开发人员编写接口文档的负担
功能测试
使用方法: SpringBoot集成Swagger
- 引入依赖,在主模块中引入该依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency>
- 在主工程的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(); } }
- 在子模块中添加配置类
@Configuration @ComponentScan("com.heima.common.swagger") public class SwaggerConfig { }
- 在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); } }
- Dto中添加注解@ApiModelProperty
@Data @EqualsAndHashCode(callSuper = true) public class ChannelDto extends PageRequestDto { /** * 频道名称 */ @ApiModelProperty(value = "频道名称") private String name; }
- 启动子模块微服务,访问地址: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
- 在主模块中的
pom.xml
文件中引入knife4j
的依赖<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> </dependency>
- 创建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(); } }
- 在子模块控制类中开启配置
@Configuration // @ComponentScan("com.heima.common.swagger") @ComponentScan("com.heima.common.knife4j") public class SwaggerConfig { }
- 在浏览器输入地址:
http://host:port/doc.html
本文含有隐藏内容,请 开通VIP 后查看