Search code examples
javajsonjerseyjackson

@JsonInclude(JsonInclude.Include.NON_NULL does not work for double


I am trying to retrieve {"status":201} in the case of route!=0 but I am getting {"status":201,"distance":0.0} how can I reach that with @JsonInclude or Jackson also without "distance":0.0?

SDBean bean = new SDBean();
if (routeD != 0) {
    // Insert the data into the bus table
    bean.status = db.insertData(macD, routeD, latD, longD);

    return Response.status(bean.status).entity(bean.toJson()).build();

}

SDBean class:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class SDBean {

    public int status;
    public ArrayList<Integer> routes;
    public double distance;

    public SDBean(){
    status = 230;
}

    public String toJson() {

        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();

        }
         System.out.println(json);
        return json;
    }
}

Solution

  • Use Double. primitives (double) can't be null (they have default values)