How can I get both the parameter in URL and the json data in content by using HttpInbound?

StackOverflow https://stackoverflow.com/questions/16812970

  •  30-05-2022
  •  | 
  •  

Question

When I use the 'Body to Parameter Map' transformer, I can get the parameter in URL, but I cannot get the content. When I use the 'JSON to Object' transformer, I can only get the content, but the parameter is lost in the payload. How can I get both of them? The configuration as follows:

<flow name="zhicall-httpFlow" doc:name="zhicall-httpFlow">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081"  path="test" encoding="UTF-8" doc:name="InHTTP" mimeType="application/json"/>
    <http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/>
    <json:json-to-object-transformer returnClass="java.util.Map" mimeType="application/json" doc:name="JSON to Object"/>
   <!--  <http:body-to-parameter-map-transformer ignoreBadInput="true" doc:name="Body to Parameter Map" encoding="UTF-8" mimeType="application/json"/> -->
    <!-- <set-property doc:name="Content-Type Property" propertyName="Content-Type" value="application/json"/> -->
    <custom-transformer encoding="UTF-8" class="com.zhicall.esb.transformer.MyTransformer" doc:name="Java" mimeType="application/json"/>
</flow>
Était-ce utile?

La solution

As per the flow provided, the first transformer transforms your payload. So by the time processing reaches the second transformer (json-to-object) the original payload is no more exists.

So try saving your original payload as a flow variable and then try applying transfomers on it.

I have modified the flow so that the original payload is saved as a flow variable. After the http transformer, the transformed payload is saved as a flow variable and the the payload is reset to the originalinput. Then again the second transformer acts on this original payload and transforms it. This way the original is transformed independently by both the transformers. And you have both transformed outputs, one as a flow variable and other as a payload after the json transformer.

<flow name="zhicall-httpFlow" doc:name="zhicall-httpFlow">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081"  path="test" encoding="UTF-8" doc:name="InHTTP" mimeType="application/json"/>

    <set-variable variableName="originalInput" value="#[payload]" />

    <http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/>

    <set-variable value="#[payload]" variableName="httpParamMap" />
    <set-payload value="#[flowVars['originalInput']]"></set-payload>

    <json:json-to-object-transformer returnClass="java.util.Map" mimeType="application/json" doc:name="JSON to Object"/>
   <!--  <http:body-to-parameter-map-transformer ignoreBadInput="true" doc:name="Body to Parameter Map" encoding="UTF-8" mimeType="application/json"/> -->
    <!-- <set-property doc:name="Content-Type Property" propertyName="Content-Type" value="application/json"/> -->
    <custom-transformer encoding="UTF-8" class="com.zhicall.esb.transformer.MyTransformer" doc:name="Java" mimeType="application/json"/>
</flow>

Hope this helps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top