使用骆驼路由提供静态文件 [英] Serving static files with camel routes

查看:28
本文介绍了使用骆驼路由提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在骆驼路由中提供静态文件.

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 显示为 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.

推荐答案

我基于 这篇博文.

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

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

它从类路径中获取需要公开的资源,并将输出消息设置为响应.请查看整个 测试用例.

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.

我测试了以下网址:

请注意,我像您一样添加了 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆