本文演示Jboot整合Swagger。
一、项目搭建
新建一个Maven项目,引入依赖:
<properties>
<jBoot.version>3.10.0</jBoot.version>
<swagger.version>1.5.21</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>io.jboot</groupId>
<artifactId>jboot</artifactId>
<version>${jBoot.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
二、添加Swagger页面资源
由于 Swagger 运行的时候,需要 html 页面来显示,因此还需要去 Swagger 的官方网站把对应的 html 资源下载下来,并放入 webapp/swaggerui 目录下。
swagger ui 的下载地址是:https://github.com/swagger-api/swagger-ui ,下载其 dist 目录即可,只需要这个目录里的文件。
请注意,webapp和src是同级目录。
三、编写测试启动类
@RequestMapping("/swagger")
@Api(description = "用户相关接口文档", basePath = "/swagger", tags = "abc")
public class MySwaggerController extends JbootController {
@ApiOperation(value = "用户列表", httpMethod = "GET", notes = "user list")
public void index() {
renderJson(Ret.ok("k1", "v1").set("name", getPara("name")));
}
@ApiOperation(value = "添加用户", httpMethod = "POST", notes = "add user")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "用户名", paramType = ParamType.FORM, dataType = "string", required = true),
@ApiImplicitParam(name = "k1", value = "k1", paramType = ParamType.FORM, dataType = "string", required = true)
})
public void add(String username) {
renderJson(Ret.ok("k1", "v1").set("username", username));
}
public static void main(String[] args) {
JbootApplication.setBootArg("jboot.swagger.path", "/swaggerui");
JbootApplication.setBootArg("jboot.swagger.title", "Jboot API 测试");
JbootApplication.setBootArg("jboot.swagger.description", "这是一个Jboot对Swagger支持的测试demo。");
JbootApplication.setBootArg("jboot.swagger.version", "1.0");
JbootApplication.setBootArg("jboot.swagger.termsOfService", "http://jboot.io");
JbootApplication.setBootArg("jboot.swagger.host", "127.0.0.1:8080");
JbootApplication.run(args);
}
}
四、启动及效果:
启动main函数并访问http://localhost:8080/swaggerui/
测试验证OK。