Swagger not working

2019-07-09 01:38发布

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

enter image description here

And checking the api-docs it only shows this:

enter image description here

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

3条回答
迷人小祖宗
2楼-- · 2019-07-09 01:43

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.

查看更多
甜甜的少女心
3楼-- · 2019-07-09 01:48

You could have a look at this article:

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;
        }
    }
    
查看更多
你好瞎i
4楼-- · 2019-07-09 01:58

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

查看更多
登录 后发表回答