JFinal整合Swagger

jfinal使用swagger的极简插件

码云地址:https://gitee.com/wenhaofan/JFinalSwagger

借鉴于:https://gitee.com/leeckent/jfinal-swagger/

个人站:http://www.wenhaofan.com/article/20190304101839

JFinalSwagger使用说明

Demo:https://gitee.com/wenhaofan/JFinalSwaggerDemo

1. 引入

    <dependency>
        <groupId>live.autu</groupId>
        <artifactId>jfinal-swagger</artifactId>
        <version>1.0.0</version>
    </dependency>

2. 下载 swagger-ui-master 将 dist 中文件加入到项目中

可配置成类似如下路径:
    webapp
        static
            swagger
                favicon-16x16.png
                ...
                swagger-ui.js.map
    WEB-INF
        views
            swagger
                index.html

3. 增加Swagger路由控制

    以第二步的形式配置的目录结构,可直接使用如下路由配置

    routes.add(new SwaggerRoutes());

    也可自行配置路由信息

    public class SwaggerRoutes extends Routes {

        @Override
        public void config() {
            setBaseViewPath("/WEB-INF/views");
            add("/swagger", SwaggerController.class);
        }

    }

4. 参数配置

  @Override
    public void configPlugin(Plugins me) {
    me.add(new SwaggerPlugin());
  }

添加配置文件 :swagger.txt

  basePath=/
  host=127.0.0.1:8080
  version=2.0
  info.description=测试jfinal swagger
  info.version=1.0
  info.title=测试

喜欢代码配置也可以使用代码配置

 @Override
    public void configPlugin(Plugins me) {
        me.add(new SwaggerPlugin(new SwaggerDoc().setBasePath("/").setHost("127.0.0.1").setSwagger("2.0")
                .setInfo(new SwaggerApiInfo("jfinal swagger demo", "1.0", "jfinal swagger", ""))));
    }

配置undertow.txt

添加

undertow.hotSwapClassPrefix=live.autu.plugin.jfinal.swagger

如过有多个Prefix则以 , 分隔

5. 添加注解

    提供五种注解,使用方式与swagger-annotation一致:

    @Api

    @ApiOperation

    @Param

    @ApiImplicitParams

    @ApiImplicitParam

    更多注解支持持续更新中

注解使用示例:

@Api(tags = "test", description = "测试")public class BlogController extends Controller {    @ApiOperation(tag = "index", httpMethod = RequestMethod.GET , description = "测试")    @ApiImplicitParams({            @ApiImplicitParam(name = "id", description = "编号", required = true, dataType = "Long"),            @ApiImplicitParam(name = "name", description = "姓名", required = true, dataType = "String")    })    public void test() {        renderJson(list);    }}

6.说明

该插件会自动读取Route配置以及@ActionKey所以无需配置url

评论区

正负余

2019-12-30 13:04

@新知 不好意思现在才看到,试试运行这个demo呢 ,导出后直接运行,https://gitee.com/wenhaofan/JFinalSwaggerDemo

正负余

2019-12-30 13:04

@onlyglume 不好意思现在才看到,试试运行这个demo呢 ,导出后直接运行,https://gitee.com/wenhaofan/JFinalSwaggerDemo

virgilian

2020-04-13 15:17

你好,我在开发环境启动,报500,请问什么原因
java.lang.RuntimeException: File not found : C:/zxk/IdeaProjects/pmms/back/Camera/WEB-INF/view/swagger/index.html

zylucky

2020-05-01 20:38

Unable to render this definition
The provided definition does not specify a valid version field.

Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).

访问报这个错。

Mavenjay

2020-08-22 17:46

@JFinal 这个怎么排序呢? 现在接口顺序错乱的 不好找

Mavenjay

2020-08-22 17:47

这个怎么排序呢? 现在接口顺序错乱的 不好找

大家快来

2020-10-17 14:15

@正负余 paramType = “header” 没有参数类型,比如在header中设置token参数

赵凯林

2022-03-01 14:29

java.lang.ClassCastException: live.autu.plugin.jfinal.swagger.config.SwaggerPlugin cannot be cast to com.jfinal.plugin.IPlugin
at com.jfinal.core.Config.startPlugins(Config.java:118)
at com.jfinal.core.Config.configPluginWithOrder(Config.java:71)
at com.jfinal.core.Config.configJFinal(Config.java:59)
at com.jfinal.core.JFinal.init(JFinal.java:61)
at com.jfinal.core.JFinalFilter.init(JFinalFilter.java:64)
at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:111)
at io.undertow.servlet.core.ManagedFilter.createFilter(ManagedFilter.java:80)
at io.undertow.servlet.core.DeploymentManagerImpl$2.call(DeploymentManagerImpl.java:594)
at io.undertow.servlet.core.DeploymentManagerImpl$2.call(DeploymentManagerImpl.java:559)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.core.DeploymentManagerImpl.start(DeploymentManagerImpl.java:601)
at com.jfinal.server.undertow.UndertowServer.configHttp(UndertowServer.java:287)
at com.jfinal.server.undertow.UndertowServer.doStart(UndertowServer.java:265)
at com.jfinal.server.undertow.UndertowServer.start(UndertowServer.java:158)
at cn.common.config.MainConfig.main(MainConfig.java:229)

热门分享

扫码入社