2017-02-21 37 views
0

我正在使用骆驼集成Swagger。我下载的dist文件夹重命名为swagger并更新了index.html,但我无法通过我的应用程序端口访问它:http://localhost:$ {port}/$ {contextPath} /swagger/index.htmlSwagger UI与骆驼集成...无法访问端口上的index.html

但if我输入http://localhost:$ {port}/api-doc,我得到了swagger json dump。

如果转到文件并用浏览器手动打开它,我可以访问index.html。如何使用我的应用程序端口访问index.html?

http://localhost:$ {端口}/$ {contextPath中} /swagger/index.html

+0

您使用的web.xml文件?如果是这样,你可以请张贴吗? – Khaled

+0

no web.xml我正在使用骆驼休息路线 – McQueen

+0

我正在使用骆驼来整合Swagger。我下载了重命名为swagger的dist文件夹并更新了index.html,但我无法通过我的应用程序端口访问它:http:// localhost:$ { port}/$ { contextPath}/swagger/index。html 但是如果我输入http:// localhost:$ { port}/api-doc,我会得到swagger json dump。 我可以访问index.html,如果转到该文件并用浏览器手动打开它。如何使用我的应用程序端口访问index.html? http:// localhost:$ { port}/$ { contextPath} /swagger/index.html – McQueen

回答

2

你需要公开的端点,将返回你的index.html文件。尝试添加以下到您RouteBuilder:

rest("/swagger") 
    .produces("text/html") 
    .get("/index.html") 
    .responseMessage().code(200).message("Swagger UI").endResponseMessage() 
    .to("direct://get/swagger/ui/path"); 

from("direct://get/swagger/ui/path") 
    .routeId("SwaggerUI") 
    .setBody().simple("resource:classpath:/swagger/index.html"); 

第一条路线定义了一个宁静的端点/swagger/index.html接受一个GET请求,并返回text/html的。

第二条路线使用Camel的Simple组件将实际的index.html读入Exchange邮件正文中。

所以,如果你去本地主机:$ {端口}/$ {contextPath中} /swagger/index.html,应该正确地加载的index.html文件。

请注意,在indext.html之内,您需要替换将返回API的JSON转储的URL,以便Swagger UI可以呈现它。

+0

随着这些变化,当我去swagger/index.html页面显示“”这就是所有。我如何获得这个路径选择放在/resources/swagger/index.html里面的swagger ui? – McQueen

+0

我想它只是创建一个没有链接到swagger UI的虚拟路由。如何将这条路线链接到swagger UI代码?这是我写的路由器: rest(“/ swagger/index.html”) .get() .to(“direct:get/swagger/index.html”); (“direct://get/swagger/index.html”) .routeId(“SwaggerUI”) .enrich(“file:// http:// localhost:{{rest.port}}/swagger/index.html“) .convertBodyTo(String.class); – McQueen

+0

java.io.FileNotFoundException:在类路径中找不到资源:classpath:index.html URI:classpath:index.html – McQueen

-1

更简单的解决办法是增加一个的web.xml文件,移动你的资源,你的/webapp的目录,在web.xml中

<filter> 
    <filter-name>RestSwaggerCorsFilter</filter-name> 
    <filter-class>org.apache.camel.swagger.servlet.RestSwaggerCorsFilter</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>RestSwaggerCorsFilter</filter-name> 
    <url-pattern>/api-docs/*</url-pattern> 
    <url-pattern>/rest/*</url-pattern> 
</filter-mapping> 

见骆驼添加以下过滤器Swagger文档的详细信息:http://camel.apache.org/swagger-java.html

+0

我在同一文档中使用了Swagger的rest-dsl。应该是相同的 – McQueen

0

我正在使用Camel Jetty作为REST组件。对于码头,您只需添加一个额外的ResourceHandler来处理SwaggerUI资源。以下使用Spring xml语言配置REST:

<restConfiguration component="jetty" host="{{rest_hostname}}" port="{{rest_listen_port}}" 
         bindingMode="off" apiContextPath="api-docs" apiContextListing="true" 
         enableCORS="true"> 
     <!--ResourceHandler to handle Swagger UI contents on the jetty--> 
     <endpointProperty key="handlers" value="swaggerUIHandler"/> 
...... 
...... 
</restConfiguration> 

我在webjars提供的jar中使用swaggerUI。配置处理器豆:

<bean id="swaggerUIHandler" class="com.yifanwu.examples.camel.JettyResourceHandlerFactory" factory-method="buildResourceHandler"> 
     <!--your resource path is just "swagger" assuming it is on the classpath--> 
     <constructor-arg name="resourcePath" value="META-INF/resources/webjars"/> 
</bean> 

bean工厂:

public class JettyResourceHandlerFactory { 
    public static ResourceHandler buildResourceHandler(String resourcePath) throws Exception { 
     ResourceHandler rh = new ResourceHandler(); 
     rh.setResourceBase(JettyResourceHandlerFactory.class.getClassLoader() 
       .getResource(resourcePath) 
       .toURI().toString()); 
     return rh; 
    } 
} 

完整的示例在这里:github