Search code examples
restletswagger

Generating Swagger 2.0 JSON from Restlet


Using Restlet 2.3.3 I've declared my Restlet Application to derive from SwaggerApplication. When I visit the default /api-docs URI, I am observing that the Swagger version of the meta data is 1.2. Subsequently, I'm not able to load this into Swagger-UI without an error.

How can I obtain Swagger 2.0 JSON from Restlet's Swagger extension? The documentation seems to suggest that it can output v2.0 JSON.

Thanks!


Solution

  • The class SwaggerApplication only supports Swagger 1.2. If you want to use Swagger 2.0, you must attach manually the corresponding restlet to generate the content based on the class Swagger2SpecificationRestlet as described below:

    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
    
        // Configuring Swagger 2 support
        Swagger2SpecificationRestlet swagger2SpecificationRestlet
                       = new Swagger2SpecificationRestlet(this);
        swagger2SpecificationRestlet.setBasePath(
                               "http://myapp.com/api/v1");
        swagger2SpecificationRestlet.attach(router);
    
        return router;
    }
    

    With such configuration, the associated resource to get the Swagger content is attached on the path /swagger.json. You can notice that you can specify your own path with the method Swagger2SpecificationRestlet#attach(Router, String).

    Hope it helps you, Thierry