Vlad K
Vlad K

Reputation: 2841

Mule object-to-json-transformer: how to exclude nulls?

Is there a way to instruct Mule object-to-json-transformer component to not include null properties into the JSON produced?

By default it produces something like this for null object properties:

"value": null

Thank you,

ps. just wanna clarify that I'm looking for a way to configure this in Mule, not by using the classes jackson annotations.

Upvotes: 2

Views: 1685

Answers (3)

Vlad K
Vlad K

Reputation: 2841

Ok, I've found the answer.

Mule object-to-json transformer is ignoring jackson annotations.

Instead, you need to define a customized object mapper bean like this:

<spring:beans>
        <spring:bean id="Bean" name="NonNullMapper" class="org.codehaus.jackson.map.ObjectMapper">
            <spring:property name="SerializationInclusion">
               <spring:value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion">NON_NULL</spring:value>
            </spring:property>
        </spring:bean>
</spring:beans>

and then reference the bean in the transformer like this:

<json:object-to-json-transformer doc:name="Object to JSON" mapper-ref="NonNullMapper"/>

Note, that this approach only works with custom java classes.

Upvotes: 3

Goes with this, @JsonSerialize (include = JsonSerialize.Inclusion.NON_NULL), simply place it in its class. The library to import is import org.codehaus.jackson.map.annotate.JsonSerialize.

Upvotes: 1

Ryan Carter
Ryan Carter

Reputation: 11606

You can configure the ObjectMapper that the json transformers use like so:

<spring:beans>
        <spring:bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
        <spring:bean
            class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
            <spring:property name="targetObject" ref="jacksonObjectMapper" />
            <spring:property name="targetMethod" value="configure" />
            <spring:property name="arguments">
                <spring:list>
                    <spring:value>WRITE_NULL_VALUES</spring:value>
                    <spring:value>false</spring:value>
                </spring:list>
            </spring:property>
        </spring:bean>
    </spring:beans>

    <flow name="json" >

        ...
        <json:object-to-json-transformer mapper-ref="jacksonObjectMapper" />

    </flow>

Or if you are using a Map:

<spring:beans>
        <spring:bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
        <spring:bean
            class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
            <spring:property name="targetObject" ref="jacksonObjectMapper" />
            <spring:property name="targetMethod" value="configure" />
            <spring:property name="arguments">
                <spring:list>
                    <spring:value>WRITE_NULL_MAP_VALUES</spring:value>
                    <spring:value>false</spring:value>
                </spring:list>
            </spring:property>
        </spring:bean>
    </spring:beans>

    <flow name="json" >

        ...
        <json:object-to-json-transformer mapper-ref="jacksonObjectMapper" />

    </flow>

Upvotes: 0

Related Questions