0
votes
<flow name="datamappingFlow1" doc:name="datamappingFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="${hostname}" port="${port}" path="account" connector-ref="ConnectorWithoutMuleSession" doc:name="HTTP"/>
    <jersey:resources doc:name="REST">
        <component class="com.mycompany.qb.rest.AccountMappingService"/>
    </jersey:resources>
    <object-to-byte-array-transformer doc:name="Object to Byte Array"/>       
    <byte-array-to-object-transformer doc:name="Byte Array to Object"/>
    <scatter-gather doc:name="upsert to db">
        <flow-ref name="test1" doc:name="Flow Reference"></flow-ref>
        <flow-ref name="test2" doc:name="Flow Reference"></flow-ref>            
    </scatter-gather>
</flow>
<flow name="test1" doc:name="test1">
    <logger message="====Input deduction data is 1======#[payload]" level="INFO" doc:name="Logger"/>
    <logger message="==== Input test1 data    ======#[message.inboundProperties['City']]" level="INFO" doc:name="Logger"/>
    <db:insert config-ref="Oracle_Configuration" doc:name="Database">
        <db:parameterized-query><![CDATA[insert into test1(City)values(#[payload.City])]]></db:parameterized-query>
    </db:insert>
</flow>
<flow name="test2" doc:name="test2">
    <logger message="====Input deduction data is 2======#[payload]" level="INFO" doc:name="Logger"/>
    <logger message="==== Input test2 data    ======#[payload.City]" level="INFO" doc:name="Logger"/>
    <db:insert config-ref="Oracle_Configuration" doc:name="Database">
        <db:parameterized-query><![CDATA[insert into test2(City)values(#[payload.City])]]></db:parameterized-query>
    </db:insert>
</flow>

Rest Component:

@Path("/")
public class AccountMappingService {  
        @POST
        @Path("/mapping")
        @Produces(MediaType.APPLICATION_JSON)
        @Consumes(MediaType.APPLICATION_JSON)
        public String dataMapping(@Payload String content){
            log.info("Rest Content is==>\n"+content);
            return content;
        }

Input data:

    {
    "City":"SFO1"
    }

Output:

====Input deduction data is 2======{
"City":"SFO1"
}
    ==== Input test1 data    ======null

====Input deduction data is 1======{
"City":"SFO1"
}
    ==== Input test2 data    ======null

why I am getting null values here?

2
Why do you have a object-to-byte-array-transformer followed by a byte-array-to-object-transformer? They are opposite transformers so having them both has a null effect (or an adverse one if the source payload is a stream, you may end up deserializing it for no reason).David Dossot

2 Answers

3
votes

==== Input test1 data ======null

Comes from:

<logger message="==== Input test1 data    ======#[message.inboundProperties['City']]" level="INFO" doc:name="Logger"/>

Inbound properties are set by the inbound endpoint. In your case, you're using an HTTP inbound endpoint. According to the doc:

To keep backward compatibility with previous versions of Mule, the headers and query parameters are also stored plain on the inbound properties.

So, unless you pass an HTTP header or query parameter named City in the HTTP request you send to your flow, the evaluation of #[message.inboundProperties['City']] can only return null.

The case of:

==== Input test2 data ======null is different. It comes from:

<logger message="==== Input test2 data    ======#[payload.City]" level="INFO" doc:name="Logger"/>

This calls .City on a payload of type String which can't work. I infer that the payload is of type String from:

public String dataMapping(@Payload String content)

You need to transform the JSON String payload into a java.util.Map if you want to extract values from it. From the doc you can see that you need to add a transformer:

<json:json-to-object-transformer returnClass="java.util.Map" />
0
votes

USe Java map and then print them using a dot operation in logger, it will work.