首页 文章

在另一个REST服务(Apache Camel)中调用REST服务

提问于
浏览
0

我使用CXF REST服务和Apache Camel实现了以下APIS .

这两个API都是 POST 类型,并且将接受 json 输入 .

  • 第一个API将采用单个 json 对象并对其执行一些操作,并将返回更新的 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>

不幸的是,我在调用此处共享的camel路由中的第一个API时收到错误 .

堆栈跟踪如下:

传递失败(ExchangeId上的消息ID:ID-localhost-35741-1477139495717-17-3:ID-localhost-35741-1477139495717-17-4) . 交付尝试后用尽:1捕获:java.lang.IllegalArgumentException:无效的uri:/ api / listAction / api / listAction . 如果要转发/桥接http endpoints ,则在 endpoints 上启用bridgeEndpoint选项:Endpoint [http:// localhost:9090 / api / compute]消息历史记录---------------- -------------------------------------------------- -------------------------------------------------- ------------------- RouteId ProcessorId Processor Elapsed(ms)[listActionRoute] [listActionRoute] [direct-vm:// listAction-service] [957] [demoRoute] [ log39] [log] [70] [demoRoute] [recipientList17] [recipientList [simple {direct-vm:$ -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 {$ }]] [23] [listActionRoute] [log36] [log] [1] [listActionRoute] [setBody16] [setBody [simple {$ {body [$ {property . CamelLoopIndex}]}}]] [2] [listActionRoute] [log37] [log] [1] [listActi onRoute] [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 ------------------------------ -------------------------------------------------- -------------------------------------------------- -----交换[Id ID-localhost-35741-1477139495717-17-4 ExchangePattern InOut Headers {Accept = text / html,application / xhtml xml,application / xml; q = 0.9,/; q = 0.8,接受 - 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,HT TP.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 = ,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 @oejshContextHandler {,null},Content-Type = application / json, org.apache.cxf.security.SecurityContext=org.apache.cxf.transport.http.AbstractHTTPDestination$2@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],co nnection = [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:无效的uri:/ api / listAction / api / listAction . 如果要转发/桥接http endpoints ,则在 endpoints 上启用bridgeEndpoint选项:Endpoint [http:// localhost:9090 / api / compute]

任何人都可以帮助我理解,为什么我会收到此错误以及如何解决此问题?

1 回答

  • 1

    我认为 Headers 混淆了 . 我想你可以通过在每次调用之间清除与 .removeHeaders("*") 类似的 Headers 来解决它 .

相关问题