java - 使用驼峰路由提供静态文件

标签 java apache-camel cxf

我正在尝试在 Camel 路由中提供静态文件。

我的主类中的路由包含这段代码:

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

这个效果很好。当我点击网址:http://xxxx:8086/user/dist/index.html时,我的索引页面将呈现,并且网址显示为http://xxxx:8086网址栏中的/user/dist/

但是当我重新加载页面(按 F5)时,网址变为:http://xxxx:8086/user/dist// 并且出现如下错误:

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 具有这种依赖性。那么我错过了什么?

我希望实现任何带有 http://clv035sl-8947d6:8888/user/dist 的 url 都应将调用路由到 index.html。为什么我需要在网址末尾明确写入 index.html

任何帮助/建议将不胜感激。

最佳答案

我制作了一个简单的 JUnit 测试用例来测试您基于 this blog post 的场景.

StaticProcessor 类的实现在哪里?我已经为这个场景实现了一些非常相似的东西(恕我直言):

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

它从类路径中获取需要公开的资源,并将输出消息设置为响应。请看一下整个test case .

我测试了以下网址:

请注意,我像您一样添加了 swagger 插件依赖项。

请告诉我这是否有帮助或指出 StaticProcessor 实现的位置,我可以用它进行测试并编辑我的答案。

干杯

关于java - 使用驼峰路由提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47406799/

相关文章:

java - Apache Camel 消费者

apache-camel - 从 Camel CXFRS 路由响应 REST 客户端?

maven - 配置 Maven 以将 CXF wsdl2java 与基本身份验证一起使用

java - apache cxf 中使用 queryParam 的方法选择

java - Apache Camel : What marches messages along?

java - 在同一场 war 中从另一个 Web 服务调用 Web 服务 - apache cxf

java - 有关Java TV API的指南

java - notify() 调用的位置重要吗?(Java)

Java 扫描器 nextInt(16) 不接受负十六进制值

java - String 到 JSONObject 并返回 String 且不丢失 UTF-8 编码