简体   繁体   中英

WSO2 ESB/EI - Pass JSON body from API to DataService

I'm trying to set up an API in Integrator that targets a DataService. I've gotten the GET routes setup, but not the PUT route.

The API accepts a large JSON string, which I pass into the API via the body of the request. It seems that I couldn't have the DataService directly get the data from the body, it looks like it HAS to be a query parameter. That's annoying, but I can deal with it, so I attempted to send the large JSON string as a query parameter to the DataService.

I couldn't figure out how to send the data as a JSON string. I can send it easily enough as XML, but the DataService is complaining that it's not receiving a string.

So, how do I send this as a JSON string? Even better - is there a way to get the DataService to accept a body payload instead of exclusively query parameters?

I'm also working with Eclipse and json-eval doesn't seem to work, so I've been avoiding it. Hoping that's not part of the problem.

json-eval(.)
Save Failed
com.jayway.jsonpath.JsonPath.compile(Ljava/lang/String;[Lcom/jayway/jsonpath/Predicate;)Lcom/jayway/jsonpath/JsonPath;

My current API resource in Integrator:

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="orders.put.IN" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
    <property expression="get-property('query.param.ID')" name="uri.var.id"/>
    <property expression="json-eval($.)" name="uri.var.full_data"/>
    <property value="1" name="uri.var.last_updated_by"/>
    <log level="custom">
        <property name="MESSAGE" value="Executing orders.put.IN sequence"/>
        <property expression="get-property('uri.var.id')" name="ID"/>
        <property expression="get-property('uri.var.full_data')" name="FULL_DATA"/>
        <property expression="get-property('uri.var.last_updated_by')" name="LAST_UPDATED_BY"/>
    </log>
    <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
    <property name="Content-Type" scope="transport" type="STRING" value="application/json"/>
    <send>
        <endpoint name="orders.put.byuserid">
            <http method="put" statistics="enable" trace="enable" 
                uri-template="https://________:8243/services/ORDERS_DataService/{uri.var.id}?LAST_UPDATED_BY={uri.var.last_updated_by}&amp;FULL_DATA={uri.var.full_data}"/>
        </endpoint>
    </send>
</sequence>

My current DataService:

<data description="____.ORDERS" disableLegacyBoxcarringMode="false" enableBatchRequests="false" enableBoxcarring="false" name="ORDERS_DataService" serviceNamespace="____" serviceStatus="active" transports="http https">
  <config enableOData="false" id="default">
    <property name="carbon_datasource_name">____</property>
  </config>
  <query id="update_ORDERS_query" useConfig="default">
    <sql>UPDATE ____.ORDERS SET FULL_DATA=?, LAST_UPADTE_DATE=SYSDATE, LAST_UPDATED_BY=? WHERE ID=?</sql>
    <param name="FULL_DATA" ordinal="1" sqlType="STRING"/>
    <param name="LAST_UPDATED_BY" ordinal="2" sqlType="STRING" />
    <param name="ID" ordinal="3" sqlType="STRING"/>
  </query>
  <resource method="PUT" path="/{ID}">
    <call-query href="update_ORDERS_query">
      <with-param name="FULL_DATA" query-param="FULL_DATA" />
      <with-param name="LAST_UPDATED_BY" query-param="LAST_UPDATED_BY" />
      <with-param name="ID" query-param="ID" />
    </call-query>
  </resource>
</data>

Full error:

java.lang.IllegalArgumentException: Value type miss match, Expected value type - '', but found - 'STRING'
        at org.apache.axis2.json.gson.GsonXMLStreamReader.nextValue(GsonXMLStreamReader.java:750)
        at org.apache.axis2.json.gson.GsonXMLStreamReader.readValue(GsonXMLStreamReader.java:625)
        at org.apache.axis2.json.gson.GsonXMLStreamReader.stateTransition(GsonXMLStreamReader.java:531)
        at org.apache.axis2.json.gson.GsonXMLStreamReader.next(GsonXMLStreamReader.java:177)
        at org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:681)
        at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:214)
        at org.apache.axiom.om.impl.llom.OMSerializableImpl.build(OMSerializableImpl.java:78)
        at org.apache.axiom.om.impl.llom.OMElementImpl.build(OMElementImpl.java:722)
        at org.apache.axiom.om.impl.llom.OMElementImpl.detach(OMElementImpl.java:700)
        at org.apache.axiom.om.impl.llom.OMNodeImpl.setParent(OMNodeImpl.java:105)
        at org.apache.axiom.om.impl.llom.OMElementImpl.addChild(OMElementImpl.java:296)
        at org.apache.axiom.om.impl.llom.OMElementImpl.addChild(OMElementImpl.java:212)
        at org.apache.axiom.soap.impl.llom.SOAPBodyImpl.addChild(SOAPBodyImpl.java:231)
        at org.apache.axis2.json.gson.JSONMessageHandler.invoke(JSONMessageHandler.java:84)
        at org.apache.axis2.engine.Phase.invokeHandler(Phase.java:340)
        at org.apache.axis2.engine.Phase.invoke(Phase.java:313)
        at org.apache.axis2.engine.AxisEngine.invoke(AxisEngine.java:261)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:167)
        at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:338)
        at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:383)
        at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:152)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)

Found the answer myself. Need to enrich the sequence and clone the body, apparently.

<enrich description="Get Body Payload from original REST request">
    <source clone="true" type="body"/>
    <target property="payload" type="property"/>
</enrich>

From there it can be used as the property named "payload"

<arg evaluator="xml" expression="get-property('payload')"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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