Preventing null values ​​inside object property using jersey

I am using jersey to serialize responses in a web service. I don't want the service to return null values, so I use the appropriate annotation, for example:

@JsonInclude(Include.NON_NULL)    
class City{
   String name;
   Stat stats;  
}

      

And the Stat class looks like this:

class Stat{
   int population;
   int femalePopulation;
   int malePopulation;
}

      

I don't want the properties from Stat to show up if they are zero. But for some reason I keep getting them. I tried using @JsonInclude(Include.NON_NULL)

in class Stat

but it doesn't work :(

Any help would be appreciated.


EDIT:

Sorry, my example was not the best. I know primitive types have default values. I tried to add annotation to the Stat class, but this is not for me: /

class Stat{
   Integer population;
   Integer femalePopulation;
   Integer malePopulation;
}

      

+3


source to share


1 answer


  • Primitives cannot be empty. They have default values ​​(int 0). Use a wrapper instead Integer

    .

  • @JsonInclude(Include.NON_NULL)

    should be in class Stat

    . I'm not sure how to enable it recursively if possible.

So just do the following and it should work (as tested)



@JsonInclude(Include.NON_NULL) 
public class Stat {

    public Integer population;
    public Integer femalePopulation;
    public Integer malePopulation;
}

      

+2


source







All Articles