Swagger不工作

时间:2016-02-21 09:10:24

标签: java swagger restlet

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

enter image description here

检查api-docs只能说明这一点:

enter image description here

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

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;
    }
}

3 个答案:

答案 0 :(得分:1)

解决方案是添加以下代码:

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

并将Swagger UI指向/swagger.json

答案 1 :(得分:1)

你可以看一下这篇文章:

Swlet 1和2都支持Restlet的Swagger扩展:

  • 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;
        }
    }
    

答案 2 :(得分:0)

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