如何在 Java 中集成 Swagger-ui for Restlet webservice?

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

我想为我们的 restlet 网络服务实施 Swagger-ui。我可以遵循任何步骤将 swagger-ui 集成到我们的项目中吗?

我试过以下代码 - 申请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;
    }
}

资源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>

但是当我 运行 项目使用 Apapche-Tomcat 并点击 url

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

它没有在浏览器中显示任何 ui。我是否遗漏了什么或者我是否遵循了任何错误的步骤?

请帮我集成 Swagger-ui for restlet webservice 您的帮助更重要。

更新

我的项目目录树看起来像 -

您的项目已经将“/*”的内容映射到 JAX-RS Servlet。

克隆 swagger-ui 并将 /dist 目录的内容复制到 WebContent 目录下的新目录中,例如 /WebContent/ui。您可能想要更改 index.html 中 SwaggerUi 对象的 url 参数以指向您的文档,在您的情况下它将是 http://localhost:8080/SimpleRestlet/api-docs

然后,在您的 web.xml 中添加以下内容以实现正确的路由:

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

UI 将托管在您提供的目录的上下文根目录下。按照上面的示例,它将托管在 http://localhost:8080/SimpleRestlet/ui.