简体   繁体   English

Apache-camel:在 http 端点上启用 bridgeEndpoint

[英]Apache-camel: Enabling bridgeEndpoint on the http endpoint

I created a simple route to get contact from a remote host.我创建了一个简单的路由来从远程主机获取联系。 But, there seems to be a lot of confusion regarding the bridgeEndpoint option.但是,对于 bridgeEndpoint 选项似乎有很多困惑。

Initially, I added the route using the Java DSL as follows:最初,我使用 Java DSL 添加了路由,如下所示:

        from("direct:getContact")
                .marshal().json(JsonLibrary.Jackson)
                .setHeader("Content-Type", constant("application/json"))
                .setHeader("Accept", constant("application/json"))
                .setHeader(Exchange.HTTP_METHOD, constant("GET"))
                .recipientList(simple("http://<remoteHost>:8080/api/contact" +
                        "/${header.contactId}"))
                .unmarshal().json(JsonLibrary.Jackson);

This route is just a proxy for the get contact API of the remote host.该路由只是远程主机的 get contact API 的代理。 I got the following error:我收到以下错误:

Invalid uri: /ib/contact/51702/contact/51702. If you are forwarding/bridging http endpoints, then enable the bridgeEndpoint option on the endpoint: Endpoint[http://<remoteHost>:8080/api/contact/51702]

/ib/* you see is the base url for the tomcat servlet.您看到的 /ib/* 是 tomcat servlet 的基本 url。 As suggested in the error, I added the bridgeEndpoint=true to the endpoint as shown below:正如错误中所建议的,我将 bridgeEndpoint=true 添加到端点,如下所示:

        from("direct:getContact")
                .marshal().json(JsonLibrary.Jackson)
                .setHeader("Content-Type", constant("application/json"))
                .setHeader("Accept", constant("application/json"))
                .setHeader(Exchange.HTTP_METHOD, constant("GET"))
                .recipientList(simple("http://<remoteHost>:8080/api/contact" +
                        "/${header.contactId}?bridgeEndpoint=true"))
                .unmarshal().json(JsonLibrary.Jackson);

Then, I get a different error:然后,我得到一个不同的错误:

org.apache.camel.component.http.HttpOperationFailedException: 
HTTP operation failed invoking 
http://<remoteHost>:8080/api/contact/51702/contact/51702 with statusCode: 404
at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:233)
at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:158)
at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:448)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
at org.apache.camel.processor.MulticastProcessor.doProcessSequential(MulticastProcessor.java:652)
at org.apache.camel.processor.MulticastProcessor.doProcessSequential(MulticastProcessor.java:580)
at org.apache.camel.processor.MulticastProcessor.process(MulticastProcessor.java:227)
at org.apache.camel.processor.RecipientList.sendToRecipientList(RecipientList.java:167)
at org.apache.camel.processor.RecipientList.process(RecipientList.java:120)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:118)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)

It is still appending "contact/51702" to the url of the remote host, which is giving 404. What am I missing here?它仍然将“contact/51702”附加到远程主机的 url,它给出了 404。我在这里缺少什么?

From the FAQ常见问题

In camel there are a number of components that use the http protocol headers to do their business.在camel 中有许多组件使用http 协议标头来处理它们的业务。

I believe your producer does it as well.我相信你的制作人也是这样做的。 So the following could solve your problem.因此,以下内容可以解决您的问题。

from("direct:getContact")
    .marshal().json(JsonLibrary.Jackson)
    .setHeader("Content-Type", constant("application/json"))
    .setHeader("Accept", constant("application/json"))
    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
    .removeHeader(Exchange.HTTP_PATH)
    .recipientList(simple("http://<remoteHost>:8080/api/contact" +
        "/${header.contactId}?bridgeEndpoint=true"))
    .unmarshal().json(JsonLibrary.Jackson);

You could also remove contact/${header.contactId} from the endpoint.您还可以从端点中删除contact/${header.contactId} As it looks redundant.因为它看起来多余。 But this depends on what you want to achieve.但这取决于您想要实现的目标。

Answer by @SubOptimal is almost correct, except it should be HTTP_URI header. @SubOptimal 的回答几乎是正确的,除了它应该是HTTP_URI标头。 From the doc :文档

If the bridgeEndpoint option is true, HttpProducer will ignore the Exchange.HTTP_URI header, and use the endpoint's URI for request.如果bridgeEndpoint 选项为true,HttpProducer 将忽略Exchange.HTTP_URI 标头,并使用端点的URI 进行请求。

Therefore there are 2 solutions:因此有2个解决方案:

  1. add .removeHeader(Exchange.HTTP_URI) to the route definition.removeHeader(Exchange.HTTP_URI)添加到路由定义
  2. add ?bridgeEndpoint=true query parameter添加?bridgeEndpoint=true查询参数

However this might not solve the problem if you have other headers that get in the way.但是,如果您有其他标题妨碍,这可能无法解决问题。 Probably that was your case, that's why removing all Camel http headers helped.可能这就是您的情况,这就是删除所有 Camel http 标头有帮助的原因。

Please be aware though that removing all headers might break your logic: for example HTTP_METHOD header is used to define the http method of the outgoing request.请注意,删除所有标头可能会破坏您的逻辑:例如HTTP_METHOD标头用于定义传出请求的 http 方法。 And it's up to you if you want to proxy the method too or not.如果您也想代理该方法,则取决于您。 You can find more in the same doc via the link above.您可以通过上面的链接在同一文档中找到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM