Java 使用驼峰路由服务静态文件
我正在尝试以驼峰路径提供静态文件 我的主类中的routes包含以下代码:Java 使用驼峰路由服务静态文件,java,apache-camel,cxf,Java,Apache Camel,Cxf,我正在尝试以驼峰路径提供静态文件 我的主类中的routes包含以下代码: 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
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显示为http://xxxx:8086/user/dist/
在url栏中
但当我重新加载页面时(按F5),url变成:http://xxxx:8086/user/dist//
我得到的错误如下:
这一页本应被招摇过市所取代。你有电话号码吗
以下是应用程序的pom.xml中对
大摇大摆的maven插件
<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>
com.github.kongchen
大摇大摆的maven插件
大摇大摆
编译
我的有效POM中存在这种依赖性。那么我错过了什么
我希望通过http://clv035sl-8947d6:8888/user/dist
应将调用路由到index.html
。为什么我需要在url的末尾明确地写index.html
任何帮助/建议都将不胜感激。我制作了一个简单的JUnit测试用例来测试您的场景
StaticProcessor
类的实现在哪里?我为这个场景实现了一些非常类似的东西(IMHO):
它从类路径获取需要公开的资源,并将out消息设置为响应。请看一下整个
我已经测试了以下URL:
StaticProcessor
实现的地方是我可以使用它进行测试并编辑我的答案
干杯嗨!您是否尝试过从“dist”中删除斜杠“/”,留下类似“jetty::{{http.port}}/user/dist?matchOnUriPrefix=true”的内容?或者,在处理servlet时,可能会在路由中出现招摇过市的情况。尝试删除它以查看结果。@Ricardozanii,我尝试删除dist后的“/”,但没有成功:(感谢您努力完成此Rnd。但我仍然不确定,我遗漏了什么?您好!问题可能出在
StaticProcessor
。实现在哪里?您能帮我处理吗?这确实很好。但是当我点击url:http://localhost:8080/user/dist/index.html
,我得到控制台中所有js文件的名称“网络”选项卡显示加载的所有js文件,包括index.html。但未提供index.html(表示内容未加载到浏览器中)。浏览器是空白的。有帮助吗?嗨!我建议您使用其他解决方案来提供静态文件,而不是尝试使用Camel自己工作。可能是Apache Httpd或NGINX。您对此静态内容的意图是什么?提供静态文件后,您需要做什么?
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");
}