users@jersey.java.net

[Jersey] Re: Upgrading from 1.2 to Jersey 1.4 breaks JSON?

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Tue, 14 Dec 2010 16:42:32 +0100

Hello Dominique,

please see inline...


On 12/09/2010 12:07 AM, Dominique Guinard wrote:
> Dear all,
>
> I upgraded from Jersey 1.2 to 1.4 and noticed that the outputted JSON
> looks rather different.
>
> Null values started to appear and some objects are represented
> differently. Generally the JSON is a lot heavier than it used to be.
>
> I read in a precedent thread ("Upgraded from Jersey 1.1 to 1.4, and
> JSON now has "null" values throughout") that this happened to other
> devs but the thread did not conclude anything.
>
> Is this an actual bug? To me it seems like Jackson fires directly when
> trying to create JSON objects, taking the precedence on JAXB.
> I attached two json files to this mail, one with jersey 1.2 one with
> 1.4 without changing anything else, just the POM.

It seems that in certain environments, the default behavior changed, but
it was not the intent.
What container/configuration do you use?

>
>
> I did not configure my app to use Jackson (although that was the idea
> to support JSONp).
>
> Is there a way to make sure Jackson does fire up? Or to configure it
> not to output null values at least?
>
> The precedent thread talked about using:
>
> MapperConfigurator configurator = new MapperConfigurator(null,
> new Annotations[] {Annotations.JAXB});
> configurator.configure(SerializationConfig.Feature.WRITE_NULL_PROPERTIES,
> false);
> _mapper = configurator.getConfiguredMapper();
>
>
> but I'm pretty clueless on where to put this code in my application.
> In any resource?

You only need to define a new provider for the object mapper context
resolver.
Following is a simple implementation:

@Provider
public class MyObjectMapperProvider implements
ContextResolver<ObjectMapper>{

     @Override
     public ObjectMapper getContext(Class<?> type) {
         final ObjectMapper myMapper = new ObjectMapper();
         myMapper.configure(Feature.WRITE_NULL_PROPERTIES, false);
         return myMapper;
     }
}

HTH,

~Jakub


>
> Any help is warmly welcomed,