ObjectToJsonTransformer in Spring 4 throws NPE when having null values
<p>I am processing a JSON message with spring integration. The message can vary in format and I am interested in a particular sub-object (called <code>payload</code>) of the whole JSON I receive. I match the Java class for that sub-object against another field (<code>type</code>) from the received message in order to know how to deserialize it. </p>

<p>To achieve this, I convert the message to a <code>java.lang.Object</code> using <code>&lt;int:json-to-object-transformer type="java.lang.Object" /&gt;</code> - this will in fact convert the initial JSON to a <code>LinkedMap</code> object which I can query for fields and perform filtering and routing within my spring integration xml - for example:</p>

<pre><code>&lt;int:recipient-list-router input-channel="jsonInputChannel" ignore-send-failures="false" default-output-channel="fallbackChannel"&gt;
  &lt;int:recipient 
    channel="validInputChannel" 
    selector-expression="payload.containsKey('type') and payload.containsKey('payload')"/&gt;
&lt;/int:recipient-list-router&gt;
</code></pre>

<p>In the above the <code>payload</code> is treated as a <code>Map</code> object. Then I extract the relevant object I need to further transform into a specific Java class:</p>

<pre><code>&lt;int:chain input-channel="validInputChannel" output-channel="objectResolveChannel"&gt;
    &lt;int:transformer expression="payload.get('payload')" /&gt;
    &lt;int:object-to-json-transformer /&gt;
&lt;/int:chain&gt;
</code></pre>

<p>Here I am getting a NPE when the payload propery of the message looks like <code>{"field1": null, "field2": "something", ....}</code></p>

<p>I had a look at the Spring 4.2.4.RELEASE source code and I can see that <code>ObjectToJsonTransformer</code> calls <code>this.jsonObjectMapper.populateJavaTypes(headers, message.getPayload());</code>
This in order calls the method implementation in <code>JsonObjectMapperAdapter</code>, which is the following:</p>

<pre><code>public void populateJavaTypes(Map&lt;String, Object&gt; map, Object object) {
    map.put(JsonHeaders.TYPE_ID, object.getClass());
    if (object instanceof Collection &amp;&amp; !((Collection) object).isEmpty()) {
        map.put(JsonHeaders.CONTENT_TYPE_ID, ((Collection) object).iterator().next().getClass());
    }
    if (object instanceof Map &amp;&amp; !((Map) object).isEmpty()) {
        map.put(JsonHeaders.CONTENT_TYPE_ID, ((Map) object).values().iterator().next().getClass());
        map.put(JsonHeaders.KEY_TYPE_ID, ((Map) object).keySet().iterator().next().getClass());
    }
}
</code></pre>

<p>You can see that when the object is treated like a Map instance, the code uses <code>getClass()</code> on the first element of the map values, but this of course can be <code>null</code> and hence the NPE is thrown.  </p>

<p>We were using Spring 3.2.1.RELEASE together with Spring Integration 2.2.6.RELEASE previously and the same Spring integration XML  configuration was working perfectly. As far as I know the problematic code above was not present in Spring 3.</p>

<p>Could someone suggest a nice workaround for this issue or a way to get back to the Spring 3 behavior?</p>