在另一个 REST 服务 (Apache Camel) 中调用一个 REST 服务
Calling a REST service inside another REST service (Apache Camel)
我使用 CXF REST 服务和 Apache Camel 实现了以下 APIS。
这两个 API 都是 POST
类型,并且将接受 json
输入。
- 首先 API 将采用单个
json
对象并对其执行一些操作,然后 return 更新 json
.
- 第二个 API 应该采用
json
先前类型的对象数组,并首先为数组中的每个对象调用 API。
在这里你可以找到我的第二条骆驼路线API。
<route id="listActionRoute" customId="true" startupOrder="1">
<from uri="direct-vm:listAction-service"/>
<log message="Mapping done ${body[0]} ${body[0].size}"/>
<setBody>
<simple>${body[0]}</simple>
</setBody>
<loop copy="true">
<simple>${body.size}</simple>
<setBody>
<simple>${body[${property.CamelLoopIndex}]}</simple>
</setBody>
<marshal>
<json library="Jackson"/>
</marshal>
<setHeader headerName="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<recipientList>
<simple>http://localhost:9090/api/compute</simple>
</recipientList>
<log message="Response is ${body}"/>
</loop>
<marshal>
<json library="Jackson"/>
</marshal>
<setHeader headerName="CamelCxfRsUsingHttpAPI">
<constant>TRUE</constant>
</setHeader>
<setHeader headerName="Accept">
<constant>application/json</constant>
</setHeader>
</route>
不幸的是,我在调用此处共享的骆驼路线中的第一个 API 时出错。
堆栈跟踪如下:
Failed delivery for (MessageId: ID-localhost-35741-1477139495717-17-3
on ExchangeId: ID-localhost-35741-1477139495717-17-4). Exhausted after
delivery attempt: 1 caught: java.lang.IllegalArgumentException:
Invalid uri: /api/listAction/api/listAction. If you are
forwarding/bridging http endpoints, then enable the bridgeEndpoint
option on the endpoint: Endpoint[http://localhost:9090/api/compute]
Message History
--------------------------------------------------------------------------------------------------------------------------------------- RouteId ProcessorId Processor
Elapsed (ms) [listActionRoute ] [listActionRoute ]
[direct-vm://listAction-service
] [ 957] [demoRoute ] [log39 ] [log
] [ 70] [demoRoute ] [recipientList17 ]
[recipientList[simple{direct-vm:${header.operationName}-service}]
] [ 887] [listActionRoute ] [log35 ] [log
] [ 846] [listActionRoute ] [setBody15 ]
[setBody[simple{${body[0]}}]
] [ 1] [listActionRoute ] [bean10 ]
[bean[com.demo.service.DemoService@638f6584]
] [ 12] [listActionRoute ] [loop8 ]
[loop[simple{${body.size}}]
] [ 23] [listActionRoute ] [log36 ] [log
] [ 1] [listActionRoute ] [setBody16 ]
[setBody[simple{${body[${property.CamelLoopIndex}]}}]
] [ 2] [listActionRoute ] [log37 ] [log
] [ 1] [listActionRoute ] [marshal17 ]
[marshal[org.apache.camel.model.dataformat.JsonDataFormat@74104d19]
] [ 2] [listActionRoute ] [setHeader26 ]
[setHeader[CamelHttpMethod]
] [ 1] [listActionRoute ] [recipientList16 ]
[recipientList[simple{http://localhost:9090/api/compute}]
] [ 15]
Exchange
--------------------------------------------------------------------------------------------------------------------------------------- Exchange[ Id ID-localhost-35741-1477139495717-17-4
ExchangePattern InOut Headers
{Accept=text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8,
accept-encoding=gzip, deflate, Accept-Language=en-US,en;q=0.5,
breadcrumbId=ID-localhost-35741-1477139495717-18-1,
CamelAcceptContentType=text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8,
CamelCxfMessage={org.apache.cxf.message.MessageFIXED_PARAMETER_ORDER=false,
org.apache.cxf.resource.method=public java.lang.String
com.demo.endpoints.DemoResource.listAction(java.util.List),
http.base.path=http://localhost:9091, HTTP.REQUEST=(POST
/api/listAction)@585231269 org.eclipse.jetty.server.Request@22e1eba5,
org.apache.cxf.transport.Destination=org.apache.cxf.transport.http_jetty.JettyHTTPDestination@26de7b21,
HTTP.CONFIG=null, jaxrs.template.parameters={FINAL_MATCH_GROUP=[/]},
org.apache.cxf.message.Message.QUERY_STRING=null,
org.apache.cxf.jaxrs.model.OperationResourceInfoStack=[org.apache.cxf.jaxrs.model.MethodInvocationInfo@63dce19e],
org.apache.cxf.message.Message.ENCODING=ISO-8859-1,
HTTP.CONTEXT=ServletContext@o.e.j.s.h.ContextHandler{,null},
Content-Type=application/json,
org.apache.cxf.security.SecurityContext=org.apache.cxf.transport.http.AbstractHTTPDestination@7f64c12c,
org.apache.cxf.continuations.ContinuationProvider=org.apache.cxf.transport.http_jetty.continuations.JettyContinuationProvider@5275a1a6,
org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8],
accept-encoding=[gzip, deflate], Accept-Language=[en-US,en;q=0.5],
connection=[keep-alive], Content-Length=[120],
content-type=[application/json], Host=[localhost:9091],
User-Agent=[Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0)
Gecko/20100101 Firefox/49.0]},
org.apache.cxf.request.url=http://localhost:9091/api/listAction,
Accept=text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8,
org.apache.cxf.request.uri=/api/listAction,
org.apache.cxf.message.Message.PATH_INFO=/api/listAction,
org.apache.cxf.transport.https.CertConstraints=null,
HTTP.RESPONSE=HTTP/1.1 200
, org.apache.cxf.request.method=POST,
org.apache.cxf.async.post.response.dispatch=true,
org.apache.cxf.message.Message.IN_INTERCEPTORS=[org.apache.cxf.transport.https.CertConstraintsInterceptor@55180f7],
HTTP_CONTEXT_MATCH_STRATEGY=stem, http.service.redirection=null,
path_to_match_slash=/api/listAction,
org.apache.cxf.configuration.security.AuthorizationPolicy=null},
CamelCxfRsOperationResourceInfoStack=[org.apache.cxf.jaxrs.model.MethodInvocationInfo@63dce19e],
CamelCxfRsResponseClass=class java.lang.String,
CamelCxfRsResponseGenericType=class java.lang.String,
CamelHttpCharacterEncoding=ISO-8859-1, CamelHttpMethod=POST,
CamelHttpPath=/api/listAction, CamelHttpUri=/api/listAction,
CamelRedelivered=false, CamelRedeliveryCounter=0,
connection=keep-alive, Content-Length=120,
Content-Type=application/json, Host=localhost:9091,
operationName=listAction, User-Agent=Mozilla/5.0 (X11; Ubuntu; Linux
x86_64; rv:49.0) Gecko/20100101 Firefox/49.0} BodyType
byte[] Body {"name":"Mr XYZ Mr","salary":100.0} ]
Stacktrace
java.lang.IllegalArgumentException: Invalid uri: /api/listAction/api/listAction. If you are forwarding/bridging http
endpoints, then enable the bridgeEndpoint option on the endpoint:
Endpoint[http://localhost:9090/api/compute]
任何人都可以帮助我理解为什么会出现此错误以及如何解决此问题?
我认为 headers 搞混了。我认为您可以通过在每次调用之间清除 headers 类似于 .removeHeaders("*")
的内容来解决它。
我使用 CXF REST 服务和 Apache Camel 实现了以下 APIS。
这两个 API 都是 POST
类型,并且将接受 json
输入。
- 首先 API 将采用单个
json
对象并对其执行一些操作,然后 return 更新json
. - 第二个 API 应该采用
json
先前类型的对象数组,并首先为数组中的每个对象调用 API。
在这里你可以找到我的第二条骆驼路线API。
<route id="listActionRoute" customId="true" startupOrder="1">
<from uri="direct-vm:listAction-service"/>
<log message="Mapping done ${body[0]} ${body[0].size}"/>
<setBody>
<simple>${body[0]}</simple>
</setBody>
<loop copy="true">
<simple>${body.size}</simple>
<setBody>
<simple>${body[${property.CamelLoopIndex}]}</simple>
</setBody>
<marshal>
<json library="Jackson"/>
</marshal>
<setHeader headerName="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<recipientList>
<simple>http://localhost:9090/api/compute</simple>
</recipientList>
<log message="Response is ${body}"/>
</loop>
<marshal>
<json library="Jackson"/>
</marshal>
<setHeader headerName="CamelCxfRsUsingHttpAPI">
<constant>TRUE</constant>
</setHeader>
<setHeader headerName="Accept">
<constant>application/json</constant>
</setHeader>
</route>
不幸的是,我在调用此处共享的骆驼路线中的第一个 API 时出错。
堆栈跟踪如下:
Failed delivery for (MessageId: ID-localhost-35741-1477139495717-17-3 on ExchangeId: ID-localhost-35741-1477139495717-17-4). Exhausted after delivery attempt: 1 caught: java.lang.IllegalArgumentException: Invalid uri: /api/listAction/api/listAction. If you are forwarding/bridging http endpoints, then enable the bridgeEndpoint option on the endpoint: Endpoint[http://localhost:9090/api/compute]
Message History --------------------------------------------------------------------------------------------------------------------------------------- RouteId ProcessorId Processor
Elapsed (ms) [listActionRoute ] [listActionRoute ] [direct-vm://listAction-service
] [ 957] [demoRoute ] [log39 ] [log
] [ 70] [demoRoute ] [recipientList17 ] [recipientList[simple{direct-vm:${header.operationName}-service}]
] [ 887] [listActionRoute ] [log35 ] [log
] [ 846] [listActionRoute ] [setBody15 ] [setBody[simple{${body[0]}}]
] [ 1] [listActionRoute ] [bean10 ] [bean[com.demo.service.DemoService@638f6584]
] [ 12] [listActionRoute ] [loop8 ] [loop[simple{${body.size}}]
] [ 23] [listActionRoute ] [log36 ] [log
] [ 1] [listActionRoute ] [setBody16 ] [setBody[simple{${body[${property.CamelLoopIndex}]}}]
] [ 2] [listActionRoute ] [log37 ] [log
] [ 1] [listActionRoute ] [marshal17 ] [marshal[org.apache.camel.model.dataformat.JsonDataFormat@74104d19]
] [ 2] [listActionRoute ] [setHeader26 ] [setHeader[CamelHttpMethod]
] [ 1] [listActionRoute ] [recipientList16 ] [recipientList[simple{http://localhost:9090/api/compute}]
] [ 15]Exchange --------------------------------------------------------------------------------------------------------------------------------------- Exchange[ Id ID-localhost-35741-1477139495717-17-4 ExchangePattern InOut Headers
{Accept=text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8, accept-encoding=gzip, deflate, Accept-Language=en-US,en;q=0.5, breadcrumbId=ID-localhost-35741-1477139495717-18-1, CamelAcceptContentType=text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8, CamelCxfMessage={org.apache.cxf.message.MessageFIXED_PARAMETER_ORDER=false, org.apache.cxf.resource.method=public java.lang.String com.demo.endpoints.DemoResource.listAction(java.util.List), http.base.path=http://localhost:9091, HTTP.REQUEST=(POST /api/listAction)@585231269 org.eclipse.jetty.server.Request@22e1eba5, org.apache.cxf.transport.Destination=org.apache.cxf.transport.http_jetty.JettyHTTPDestination@26de7b21, HTTP.CONFIG=null, jaxrs.template.parameters={FINAL_MATCH_GROUP=[/]}, org.apache.cxf.message.Message.QUERY_STRING=null, org.apache.cxf.jaxrs.model.OperationResourceInfoStack=[org.apache.cxf.jaxrs.model.MethodInvocationInfo@63dce19e], org.apache.cxf.message.Message.ENCODING=ISO-8859-1, HTTP.CONTEXT=ServletContext@o.e.j.s.h.ContextHandler{,null}, Content-Type=application/json, org.apache.cxf.security.SecurityContext=org.apache.cxf.transport.http.AbstractHTTPDestination@7f64c12c, org.apache.cxf.continuations.ContinuationProvider=org.apache.cxf.transport.http_jetty.continuations.JettyContinuationProvider@5275a1a6, org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8], accept-encoding=[gzip, deflate], Accept-Language=[en-US,en;q=0.5], connection=[keep-alive], Content-Length=[120], content-type=[application/json], Host=[localhost:9091], User-Agent=[Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0]}, org.apache.cxf.request.url=http://localhost:9091/api/listAction, Accept=text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8, org.apache.cxf.request.uri=/api/listAction, org.apache.cxf.message.Message.PATH_INFO=/api/listAction, org.apache.cxf.transport.https.CertConstraints=null, HTTP.RESPONSE=HTTP/1.1 200, org.apache.cxf.request.method=POST, org.apache.cxf.async.post.response.dispatch=true, org.apache.cxf.message.Message.IN_INTERCEPTORS=[org.apache.cxf.transport.https.CertConstraintsInterceptor@55180f7], HTTP_CONTEXT_MATCH_STRATEGY=stem, http.service.redirection=null, path_to_match_slash=/api/listAction, org.apache.cxf.configuration.security.AuthorizationPolicy=null}, CamelCxfRsOperationResourceInfoStack=[org.apache.cxf.jaxrs.model.MethodInvocationInfo@63dce19e], CamelCxfRsResponseClass=class java.lang.String, CamelCxfRsResponseGenericType=class java.lang.String, CamelHttpCharacterEncoding=ISO-8859-1, CamelHttpMethod=POST, CamelHttpPath=/api/listAction, CamelHttpUri=/api/listAction, CamelRedelivered=false, CamelRedeliveryCounter=0, connection=keep-alive, Content-Length=120, Content-Type=application/json, Host=localhost:9091, operationName=listAction, User-Agent=Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0} BodyType
byte[] Body {"name":"Mr XYZ Mr","salary":100.0} ]Stacktrace
java.lang.IllegalArgumentException: Invalid uri: /api/listAction/api/listAction. If you are forwarding/bridging http endpoints, then enable the bridgeEndpoint option on the endpoint: Endpoint[http://localhost:9090/api/compute]
任何人都可以帮助我理解为什么会出现此错误以及如何解决此问题?
我认为 headers 搞混了。我认为您可以通过在每次调用之间清除 headers 类似于 .removeHeaders("*")
的内容来解决它。