招摇不工作

Swagger not working

我在使用 Restlet 制作 Swagger 显示 API 文档时遇到了一些麻烦。 Swagger 显示的只是这些东西:

检查 api-docs 它只显示了这个:

我想知道我的代码有什么问题:

public class MyApplication extends SwaggerApplication {
    private static final String ROOT_URI = "/";
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach(ROOT_URI, RootServerResource.class);
        router.attach(ROOT_URI + "ping", PingServerResource.class);
        router.attach(ROOT_URI + "ping/", PingServerResource.class);
        // Some code omitted for simplicity
        return router;
    }
}

Swagger 需要找到您的 API 操作。我不确定 Restlet,在泽西岛,你用 @Api 注释你的 REST 资源 类,用 @ApiOperation 注释你的方法。在这里阅读更多 in the swagger docs.

解决方法是添加这段代码:

    // Configuring Swagger 2 support
    Swagger2SpecificationRestlet swagger2SpecificationRestlet
            = new Swagger2SpecificationRestlet(this);
    swagger2SpecificationRestlet.setBasePath("http://localhost:8080/api-docs");
    swagger2SpecificationRestlet.attach(router);

并将 Swagger UI 指向 /swagger.json

你可以看看这篇文章:

Restlet 的 Swagger 扩展支持 Swagger1 和 2:

  • Swagger v1

    public class ContactsApplication extends SwaggerApplication {
        public Restlet createInboundRoot() {
            Router router = new Router();
            (...)
            attachSwaggerSpecificationRestlet(router, "/docs");
    
            return router;
        }
    }
    
  • Swagger v2

    public class ContactsApplication extends Application {
       public Restlet createInboundRoot() {
            Router router = new Router();
            (...)
            Swagger2SpecificationRestlet swagger2SpecificationRestlet
                                   = new Swagger2SpecificationRestlet(this);
            swagger2SpecificationRestlet.setBasePath("http://myapp.org/");
            swagger2SpecificationRestlet.attach(router, "/docs");
            return router;
        }
    }