我创建了一个简单的路由来从远程主机获取联系。但是,关于 bridgeEndpoint 选项似乎存在很多混淆。
最初,我使用 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);
此路由只是远程主机获取联系人 API 的代理。 我收到以下错误:
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/* 您看到的是 tomcat servlet 的基本 url。根据错误中的建议,我将 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);
然后,我得到一个不同的错误:
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)
它仍在将“contact/51702”附加到远程主机的 url,这给出了 404。 我在这里缺少什么?
最佳答案
来自FAQ
In camel there are a number of components that use the http protocol headers to do their business.
我相信您的制作人也会这样做。因此,以下内容可以解决您的问题。
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);
您还可以从端点中删除 contact/${header.contactId}
。因为它看起来多余。但这取决于您想要实现的目标。
关于java - Apache-camel:在 http 端点上启用 bridgeEndpoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37586488/