X

Angelo Santagata's Blog

  • November 12, 2014

Got problems with Nulls with ServiceCloud's objects in REST?

Angelo Santagata
Architect
Using Oracle RightNow, Jersey 1.18, JAX-WS, JDeveloper 11.1.1.7.1

Whilst trying to creating a REST interface to our RightNow instance for a mobile application I was hitting an issue with null values being rendered by Jersey (Jackson)

The way I access RightNow is via JAX-WS  generated proxies which generates JAXB Objects. In the past I've been able to simply return the JAXB object to Jersey and it gets rendered all lovely jubbly.. However with the RightNow WSDL I'm getting lots of extra (null) fields rendered.. This doesn't occur with Sales Cloud so I'm assuming its something to do with the WSDL definition..

     
{ "incidents" : [{"Asset" : null, "AssignedTo" : {"Account" : null, "StaffGroup" : {"ID" : {"id" : 100885},"Name" : "B2CHousewares"},"ValidNullFields" : null},"BilledMinutes" : null, "Category" : {"ID" : {"id" : 124},"Parents" : [{.....
Look at all those "null" values, yuck...

Thankfully I found a workaround (yay!), I simply needed to create a custom Object Mapper and tell it *not* to render nulls. This worked for both the JAXB objects which were generated for me and other classes

Simply create a class which overrides the normal object Mapper factory and to make sure its used, ensure the @Provider tag is present

package myPackage;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
@Provider
public class CustomJSONObjectMapper implements ContextResolver<ObjectMapper> {
private ObjectMapper objectMapper;
public CustomJSONObjectMapper() throws Exception {
System.out.println("My object mapper init");
objectMapper= new ObjectMapper();
// Force all conversions to be NON_NULL for JSON
objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
}
public ObjectMapper getContext(Class<?> objectType) {
System.out.println("My Object Mapper called");
return objectMapper;
}
}

And the result is lovely.. No null values and ready for my mobile app to consume .... 
{"organization" : [{"id" :  {"id" : 68},"lookupName" : "AngieSoft", "createdTime" : 1412166303000, "updatedTime" : 1412166303000, "name" : "AngieSoft", "salesSettings" :  {"salesAccount" :  {"id" :  {"id" : 2}},"totalRevenue" :  {"currency" :  {"id" :  {"id" : 1}}}},"source" :  {"id" :  {"id" : 1002},"parents" : [{"id" :  {"id" : 32002}}
]},"crmmodules" : {"marketing" : true, "sales" : true, "service" : true}}
]
}

Oh heads up Im using Jersey 1.18 because I want to deploy it to Oracle Java Cloud Service, if your using Jersey 2.x I believe the setSerializationInclusion method has changed..

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.