在jfinal项目中整合swagger
参考链接
https://jfinal.com/share/1455
下载jfinal-swagger-api下载到本地,制作成jar包
源码下载地址:https://gitee.com/luketech/jfinal-swagger-api
依赖
<dependency> <groupId>com.jfinal.swagger</groupId> <artifactId>jfinal-swagger-api</artifactId> <version>1.0.0</version> </dependency>
新建工程
jfinal-4.9-jfinal-swagger-api
添加依赖
<artifactId>jfinal-4.9-jfinal-swagger-api</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal-undertow</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal</artifactId> <version>4.9.12</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.16</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>com.jfinal.swagger</groupId> <artifactId>jfinal-swagger-api</artifactId> <version>1.0.0</version> </dependency> </dependencies>
启动类
package com.litongjava.jfinal.swagger; import com.jfinal.server.undertow.UndertowServer; public class App { public static void main(String[] args) { UndertowServer.start(AppConfig.class); } }
配置类
package com.litongjava.jfinal.swagger; import com.jfinal.config.Constants; import com.jfinal.config.Handlers; import com.jfinal.config.Interceptors; import com.jfinal.config.JFinalConfig; import com.jfinal.config.Plugins; import com.jfinal.config.Routes; import com.jfinal.swagger.config.SwaggerConfig; import com.jfinal.swagger.config.SwaggerRoute; import com.jfinal.swagger.enums.InType; import com.jfinal.template.Engine; public class AppConfig extends JFinalConfig { /** * 配置访问路由 * * @param me */ @Override public void configRoute(Routes me) { String basePackage="com.litongjava.jfinal.swagger"; me.scan(basePackage+".controller."); // 配置Swagger UI访问路由 me.add(new SwaggerRoute()); } /** * JFinal启动完成后的操作 */ @Override public void onStart() { // 添加全局参数(如token,非必需) SwaggerConfig.addGlobalParam("x-access-token", "token令牌", "string", "jfinal-awesome-token", true, "", InType.HEADER); } public void configConstant(Constants me) { //me.setDevMode(true); // 开启对 jfinal web 项目组件 Controller、Interceptor、Validator 的注入 me.setInjectDependency(true); // 开启对超类的注入。不开启时可以在超类中通过 Aop.get(...) 进行注入 me.setInjectSuperClass(true); } @Override public void configEngine(Engine me) { } @Override public void configPlugin(Plugins me) { } @Override public void configInterceptor(Interceptors me) { } @Override public void configHandler(Handlers me) { } }
Controller添加注解: 常用极少配置的Demo:
package com.litongjava.jfinal.swagger.controller; import com.jfinal.core.Controller; import com.jfinal.core.Path; import com.jfinal.kit.Kv; import com.jfinal.swagger.annotation.ActionApi; import com.jfinal.swagger.annotation.Api; import com.jfinal.swagger.annotation.Param; @Api(remark = "UserController,排序到末尾", sort = 2) @Path("user") public class UserController extends Controller { @ActionApi(remark = "DemoController首页Action") public void index() { renderJson(Kv.by("user", "Hello JFinal!")); } @ActionApi(remark = "输出文本") @Param(name = "text", remark = "文本内容") public void text() { renderText("输出接收文本:<br>" + get("text")); } }
所有支持的配置Demo演示(大部分属性可以使用默认或未指定时会自动设定,这里仅做演示):
package com.litongjava.jfinal.swagger.controller; import java.util.Arrays; import com.jfinal.core.Controller; import com.jfinal.core.Path; import com.jfinal.kit.Kv; import com.jfinal.swagger.annotation.ActionApi; import com.jfinal.swagger.annotation.Api; import com.jfinal.swagger.annotation.Param; import com.jfinal.swagger.annotation.Params; import com.jfinal.swagger.enums.InType; @Api(tag = "/", remark = "首页Controller", sort = 10, outerRemark = "查看更多关于首页", outerUrl = "https://www.baidu.com/s?wd=JFinal") @Path("/") public class IndexController extends Controller { @ActionApi(tag = "/", remark = "IndexController首页Action", summary = "首页", sort = 2) public void index() { renderJson(Kv.by("index", "Hello JFinal!")); } @ActionApi(tag = "/", remark = "欢迎加入JFinal俱乐部", httpMethod = "post", sort = 1, consumes = "application/json", url = "/club") @Params({ @Param(name = "memberName", remark = "会员", dataType = "string", defaultValue = "James", required = true, in = InType.QUERY, format = "", schema = "http"), @Param(name = "memberId", remark = "会员ID", dataType = "integer", defaultValue = "88888", required = true, minimum = "1", maximum = "999999"), @Param(name = "level", remark = "会员等级", dataType = "integer", defaultValue = "8") }) public void club() { String memberName = get("memberName"); int memberId = getInt("memberId"); int level = getInt("level"); Kv cnKv = Kv.by("welcome", memberName + ":欢迎加入JFinal俱乐部!").set("memberId", memberId).set("level", level); Kv enKv = Kv.by("welcome", memberName + ":Welcome to JFinal club!").set("memberId", memberId).set("level", level); renderJson(Arrays.asList(cnKv, enKv)); } }
整合结果是无法访问静态文件
静态页面被打包到了META-INF/resources/swager目录下
配置静态目录,访问jar包的META-INF/resources/下面的文件
#自动热加载 undertow.devMode=true undertow.port=8000 undertow.resourcePath = classpath:META-INF/resources,jfinal-4.9-jfinal-swagger-api
再次访问显示如下
http://localhost:8000/swagger/default/index.html
需要手动将jfinal-swagger-api打包,比较麻烦,希望詹总可以将jfinal-swagger-api推送到mavenCenter