API接口文档利器:Swagger
Swagger介绍
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务
(https://swagger.io/)。 它的主要作用是:
使得前后端分离开发更加方便,有利于团队协作
接口的文档在线自动生成,降低后端开发人员编写接口文档的负担
功能测试
Spring已经将Swagger纳入自身的标准,建立了Spring-swagger项目,现在叫Springfox。通过在项目中引入Springfox ,即可非常简单快捷的使用Swagger。
SpringBoot集成Swagger
项目中添加依赖
1 2 3 4 5 6 7 8 9 10
| <!‐‐ Swagger依赖 ‐‐> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox‐swagger2</artifactId> </dependency>
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox‐swagger‐ui</artifactId> </dependency>
|
工程的config包中添加一个Swagger配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package com.shanjupay.merchant.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 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;
@Configuration @ConditionalOnProperty(prefix = "swagger",value = {"enable"},havingValue = "true") @EnableSwagger2 public class SwaggerConfiguration {
@Bean public Docket buildDocket() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(buildApiInfo()) .select() // 要扫描的API(Controller)基础包
.apis(RequestHandlerSelectors.basePackage("com.shanjupay.merchant.controller")) .paths(PathSelectors.any()) .build(); }
/** *@param
*@return springfox.documentation.service.ApiInfo *@Title: 构建API基本信息 *@methodName: buildApiInfo */ private ApiInfo buildApiInfo() { Contact contact = new Contact("开发者","",""); return new ApiInfoBuilder() .title("闪聚支付‐商户应用API文档") .description("") .contact(contact) .version("1.0.0").build(); }
|
添加SpringMVC配置类:WebMvcConfig,让外部可直接访问Swagger文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.shanjupay.merchant.config; import org.springframework.stereotype.Component; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Component public class WebMvcConfig implements WebMvcConfigurer { /** *添加静态资源文件,外部可以直接访问地址 * *@param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger‐ui.html") .addResourceLocations("classpath:/META‐INF/resources/");
registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META‐INF/resources/webjars/"); } }
|
Swagger常用注解
在Java类中添加Swagger的注解即可生成Swagger接口文档,常用Swagger注解如下:
@Api:修饰整个类,描述Controller的作用 @ApiOperation:描述一个类的一个方法,或者说一个接口@ApiParam:单个参数的描述信息
@ApiModel:用对象来接收参数
@ApiModelProperty:用对象接收参数时,描述对象的一个字段@ApiResponse:HTTP响应其中1个描述
在Java类中添加Swagger的注解即可生成Swagger接口文档,常用Swagger注解如下:
@Api:修饰整个类,描述Controller的作用 @ApiOperation:描述一个类的一个方法,或者说一个接口@ApiParam:单个参数的描述信息
@ApiModel:用对象来接收参数
@ApiModelProperty:用对象接收参数时,描述对象的一个字段@ApiResponse:HTTP响应其中1个描述
属性 |
取值 |
作用 |
paramType |
|
查询参数类型 |
|
path |
以地址的形式提交数据 |
|
query |
直接跟参数完成自动映射赋值 |
|
body |
以流的形式提交 仅支持POST |
|
header |
参数在request headers 里边提交 |
|
form |
以form表单的形式提交 仅支持POST |
dataType |
|
参数的数据类型 只作为标志说明,并没有实际验证 |
|
Long |
|
|
String |
|
name |
|
接收参数名 |
value |
|
接收参数的意义描述 |
required |
|
参数是否必填 |
|
true |
必填 |
|
false |
非必填 |
defaultValue |
|
默认值 |
上边的属性后边编写程序时用到哪个我再详细讲解,下边写一个swagger的简单例子,我们在MerchantController 中添加Swagger注解,代码如下所示:
1 2 3 4 5
| @Api(value = "商户平台‐商户相关", tags = "商户平台‐商户相关", description = "商户平台‐商户相关") @RestController public class MerchantController {
@Reference private MerchantService merchantService;
|
Swagger生成API文档的工作原理:
1、shanjupay-merchant-application启动时会扫描到SwaggerConfiguration类
2、在此类中指定了扫描包路径com.shanjupay.merchant.controller,会找到在此包下及子包下标记有@RestController注解的controller类
3、根据controller类中的Swagger注解生成API文档
接口调试利器Postman
Postman是一款功能强大的http接口测试工具,使用Postman可以完成http各种请求的功能测试。作为服务器端 开发人员,当一个业务功能开发完毕后,应该用Postman进行功能测试。
1、请自行在本机安装Postman
2、新建集合(建议一个微服务新建一个对应的集合):商户应用


填写新建商户接口地址和请求类型后,点击Send发送请求
