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:

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.


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;



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)

public class Stat {

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




All Articles