Swagger not working

2019-07-09 01:30发布

问题:

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.