使用骆驼路线提供静态文件 [英] Serving static files with camel routes
问题描述
我正在尝试在骆驼路线中提供静态文件.
I am trying to serve a static file in camel routes.
主类中的路由包含以下代码:
The routes in my main class contains this piece of code:
public final void configure() throws Exception {
// declaring camel routes
// match on uri prefix must be true when parameters are passed as part of the uri
// for example, "http://localhost/hello/rick"
// http.port is in local.properties file user-api
from("jetty:http://0.0.0.0:{{http.port}}/user/dist/?matchOnUriPrefix=true")
.process( new StaticProcessor( "help", "index.html", "dist"))
.routeId( "static");
from("jetty:http://0.0.0.0:{{http.port}}/user?matchOnUriPrefix=true")
.to("cxfbean:userService");
}
这很好.当我按下url:http://xxxx:8086/user/dist/index.html
时,将显示我的索引页,并且该URL在URL栏中显示为http://xxxx:8086/user/dist/
.
This works good. When I hit the url: http://xxxx:8086/user/dist/index.html
, my index page is rendered and the url shows to behttp://xxxx:8086/user/dist/
in url bar.
但是当我重新加载页面(按F5键)时,URL变为:http://xxxx:8086/user/dist//
,并且出现如下错误:
But when I reload the page (press F5), the url becomes: http://xxxx:8086/user/dist//
and I get error like:
该页面应该已经被Swagger取代.你有吗 在您应用程序的pom.xml中作为以下内容的唯一参考 swagger-maven-plugin?
This page should have been replaced by Swagger. Do you have the following in your application's pom.xml as the only reference to the swagger-maven-plugin?
<build>
<plugins>
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<executions>
<execution>
<id>swagger</id>
<phase>compile</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
我在有效的POM中具有这种依赖性.那我想念什么呢?
I have this dependency in my effective POM. So what am I missing?
我希望实现任何带有http://clv035sl-8947d6:8888/user/dist
的URL都应将调用路由到index.html
.为什么我需要在URL末尾明确写index.html
?
I wish to achieve that any url with http://clv035sl-8947d6:8888/user/dist
should route the call to index.html
. Why I need to explictly write index.html
at end of the url?
任何帮助/建议将不胜感激.
Any help/ suggestion will be appreciated.
推荐答案
I made a simple JUnit Test case to test your scenario based on this blog post.
StaticProcessor
类的实现在哪里?对于这种情况,我已经实现了一些非常相似的内容(IMHO):
Where are the implementation of the StaticProcessor
class? I've implemented something for this scenario that is quite similar (IMHO):
public void configure() throws Exception {
from("jetty:http://0.0.0.0:8080/user/dist?matchOnUriPrefix=true").process(new Processor() {
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
String relativepath = in.getHeader(Exchange.HTTP_PATH, String.class);
String requestPath = in.getHeader("CamelServletContextPath", String.class); //CamelServletContextPath
if (relativepath.isEmpty() || relativepath.equals("/")) {
relativepath = "index.html";
}
final String formattedPath = String.format("%s/%s", requestPath, relativepath);
InputStream pathStream = this.getClass().getResourceAsStream(formattedPath);
Path path = FileSystems.getDefault().getPath(this.getClass().getResource(formattedPath).getPath());
Message out = exchange.getOut();
try {
out.setBody(IOUtils.toByteArray(pathStream));
out.setHeader(Exchange.CONTENT_TYPE, Files.probeContentType(path));
} catch (IOException e) {
out.setBody(relativepath + " not found.");
out.setHeader(Exchange.HTTP_RESPONSE_CODE, "404");
}
}
}).routeId("static");
}
它从类路径中获取需要公开的资源,并将out消息设置为响应.请查看整个测试用例.
It takes from the classpath the resources that needed to be exposed and set the out message to the response. Please, take a look at the entire test case.
我已经测试了以下URL:
I've tested the following URLs:
- http://localhost:8080/user/dist/
- http://localhost:8080/user/dist
- http://localhost:8080/user/dist/index.html
- http://localhost:8080/user/dist/
- http://localhost:8080/user/dist
- http://localhost:8080/user/dist/index.html
请注意,就像您一样,我添加了swagger插件依赖项.
Please note that I added the swagger plugin dependency just like you did.
让我知道这对您有帮助还是指出StaticProcessor
实现的目的,以便我可以对其进行测试并修改答案.
Let me know if that helps or point where the StaticProcessor
implementation is that I may test with it and edit my answer.
欢呼
这篇关于使用骆驼路线提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!