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

时间:2015-02-18 06:25:23

标签: java rest restlet swagger swagger-ui

我想为 restlet webservices实施 Swagger-ui 。是否有任何步骤可以将swagger-ui集成到我们的项目中?

我试过以下代码 - 申请类

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

资源类

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 运行项目并点击网址时

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

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

请帮我整合Swagger-ui for restlet webservice 你的帮助更重要。

更新

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

enter image description here

1 个答案:

答案 0 :(得分:1)

您的项目已将'/ *'的内容映射到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