市面上有很多遵循 OpenApi 规范的自动化接口文档生成的解决方案。下面介绍两种:
/**
* @apiGroup Product
* @api {GET} /product/{id} 查询一个产品
* @apiDescription 接口描述xxx
* @apiParam {String} id 产品id(必填*)
* @apiSuccessExample SuccessExample
* HTTP/1.1 200
* {
* id: 'xxx',
* name: 'xxx',
* desc: 'xxxx'
* }
* @apiErrorExample ErrorExample
*/
@GetMapping("/{id}")
public Product detail(@PathVariable String id)
{
return JsonData.buildSuccess();
}
优点
缺点
Swagger 丝袜哥
RestController
@RequestMapping("api/v1/user")
@Api(tags = "用户模块",value = "用户UserController")
public class UserController {
@Autowired
private BannerService bannerService;
@ApiOperation("分页用户列表")
@GetMapping("list")
public JsonData list(){
List<BannerDO> list = bannerService.list();
return JsonData.buildSuccess(list);
}
}
swagger2的整合和swagger3有一些区别。详细的可以查看以前的文档。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
# 应用名称
spring.application.name=shop
# ===== 自定义swagger配置 ===== #
# 是否启用,测试和开发环境启用,生存环境关闭
swagger.enable=true
# 引用应用名称
swagger.application-name= ${spring.application.name}
#版本
swagger.application-version=1.0
#说明
swagger.application-description=shop api info
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
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;
@Component
@EnableOpenApi
@ConfigurationProperties("swagger")
@Data
public class SwaggerConfiguration {
/**
* 是否开启swagger,生产环境一般关闭,所以这里定义一个变量
*/
private Boolean enable;
/**
* 项目应用名
*/
private String applicationName;
/**
* 项目版本信息
*/
private String applicationVersion;
/**
* 项目描述信息
*/
private String applicationDescription;
@Bean
public Docket docket() {
return new Docket(DocumentationType.OAS_30)
.pathMapping("/")
// 定义是否开启swagger,false为关闭,可以通过变量控制,线上关闭
.enable(enable)
//配置api文档元信息
.apiInfo(apiInfo())
// 选择哪些接口作为swagger的doc发布
.select()
//apis() 控制哪些接口暴露给swagger,
// RequestHandlerSelectors.any() 所有都暴露
// RequestHandlerSelectors.basePackage("net.xdclass.*") 指定包位置
// withMethodAnnotation(ApiOperation.class)标记有这个注解 ApiOperation
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(applicationName)
.description(applicationDescription)
.contact(new Contact("youyou", "https://baidu.com", "[email protected]"))
.version(applicationVersion)
.build();
}
}
@Api(tags = "用户模块",value = "用户UserController")
public class UserController {
}
@ApiOperation("分页用户列表")
@GetMapping("list")
public JsonData list(){
return JsonData.buildSuccess();
}
@ApiOperation("用户登录")
@PostMapping("login")
public JsonData login(
@ApiParam(name = "phone", value = "手机号",example = "13888888888")
@RequestParam("phone") String phone,
@ApiParam(name = "pwd", value = "密码",example = "123456")
@RequestParam("pwd")String pwd){
return JsonData.buildSuccess();
}
@ApiOperation("删除用户")
@DeleteMapping("/delete/{id}")
public JsonData deleteById(@PathVariable int id) {
return JsonData.buildSuccess();
}
@ApiIgnore
@ApiOperation("删除用户")
@DeleteMapping("/delete/{id}")
public JsonData deleteById(@PathVariable int id) {
return JsonData.buildSuccess();
}
@Data
@ApiModel("用户基本信息")
public class SaveUserRequest {
private int age;
private String pwd;
@ApiModelProperty(value ="【必填】邮箱",required = true)
private String email;
@ApiModelProperty("【必填】手机号")
private String phone;
@ApiModelProperty(value="创建时间")
private Date createTime;
}