Search code examples
javarestrestletswaggerswagger-ui

How to integrate Swagger-ui for Restlet webservice in Java?


I want to implement Swagger-ui for our restlet webservices. Is there any steps that can I follow to integrate swagger-ui in our project?

I have tried following code - Application class

public class DemoApplication extends SwaggerApplication {

    public Restlet createInboundRoot() {       

        Router baseRouter = new Router(getContext());
        DemoResource demoRestlet = new DemoResource(
                getContext());
        demoRestlet.setApiInboundRoot(this);
        attachSwaggerDocumentationRestlets(baseRouter, "/api-docs",
                demoRestlet, "/api-docs/{demo}", demoRestlet);
        return baseRouter;
    }
}

Resource Class

public class DemoResource extends SwaggerSpecificationRestlet {

    public DemoResource(Context context) {
        super(context);
    }

    @Override
    public Representation getApiDeclaration(String category) {
        try {
            ApiDeclaration apiDeclaration = new JacksonRepresentation<ApiDeclaration>(
                    super.getApiDeclaration(category), ApiDeclaration.class)
                    .getObject();
            // manipulate the API declaration object as you wish
            apiDeclaration.setBasePath("demo");
            return new JacksonRepresentation<ApiDeclaration>(apiDeclaration);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SimpleRestlet</display-name>

<!-- Application class name -->
    <context-param>
        <param-name>org.restlet.application</param-name>
        <param-value>
            deepu.example.DemoApplication
        </param-value>
    </context-param>

    <!-- Restlet adapter -->
    <servlet>
        <servlet-name>RestletServlet</servlet-name>
        <servlet-class>
        org.restlet.ext.servlet.ServerServlet
        </servlet-class>
    </servlet>

    <!-- Catch all requests -->
    <servlet-mapping>
        <servlet-name>RestletServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

But when I run the project using Apapche-Tomcat and hit the url

http://localhost:8080/SimpleRestlet/api-docs

It is not showing any ui in browser. Do I missing anything or am I following any wrong steps?

Please help me to integrate Swagger-ui for restlet webservice Your help matters more.

UPDATE

My project's directory tree looks like -

enter image description here


Solution

  • Your project already maps the content of '/*' to the JAX-RS Servlet.

    Clone swagger-ui and copy the contents of the /dist directory into a new directory under your WebContent directory, for example /WebContent/ui. You'd probably want to change the url parameter of the SwaggerUi object in the index.html to point at your documentation, in your case it would be http://localhost:8080/SimpleRestlet/api-docs.

    Then, to your web.xml add the following for proper routing:

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/ui/*</url-pattern>
    </servlet-mapping>
    

    The UI will be hosted under the context root at the directory you gave it. Following the example above, it would be hosted under http://localhost:8080/SimpleRestlet/ui.