Swagger not working

为君一笑 提交于 2019-12-11 01:45:15

问题


I'm having a bit of trouble making Swagger display API docs using Restlet. What Swagger shows is just these stuff:

And checking the api-docs it only shows this:

I wonder what is wrong with my code:

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

回答1:


You could have a look at this article:

  • What can APISpark bring to your existing Web APIs (Part 2) -http://restlet.com/blog/2016/01/04/what-can-apispark-bring-to-your-existing-web-apis-part-2/

Both Swagger1 and 2 are supported by the Swagger extension of Restlet:

  • 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:


The solution is to add this code:

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

And point the Swagger UI to /swagger.json




回答3:


Swagger needs to find your API operations. I'm not sure about Restlet, in Jersey you annotate your REST resource classes with @Api and your methods with @ApiOperation. Read more here in the swagger docs.



来源:https://stackoverflow.com/questions/35534281/swagger-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!