0

Response.java

public class Response{

private String mobileNo;
private String contractId;
private String sim;
private String imei;

public Response(String mobileNo, String contractId){
    this.mobileNo = mobileNo;
    this.contractId = contractId;

}

public Response(String mobileNo, String contractId, String sim,
        String imei, String identificationType) {
    this.mobileNo = mobileNo;
    this.contractId = contractId;
    this.sim = sim;
    this.imei = imei;
    this.identificationType = identificationType;
}



//Getter and Setter

}

MainEx.java

public class MainEx{

   Response  response = null;  

   public Response response(){

     String mobileNo = null;
     String contractId = null;
     String sim = null;
     String imei = null;

     if(something){
        response= new IVRAccountDetailsRs("777","4545"); 
     }
     else{
        response= new IVRAccountDetailsRs("777","4545","sim","imei");
     }
    return response;
   }
}

When if statement call return response as

{ "mobileNo" = "777";
  "contractId" = "4545";
  "sim"= null;
  "imei" = null;
}

But I want to get the response as bellow,

When calling if statement

Need to remove other two values.

{ "mobileNo" = "777";
  "contractId" = "4545";
}

If contractId and mobileNo null then output should be

{ "mobileNo" = null;
  "contractId" = null;
}

When calling else statement

{ "mobileNo" = "777";
  "contractId" = "4545";
  "sim"= "sim";
  "imei" = "imei";
}

if all values null

 { "mobileNo" = null;
      "contractId" = null;
      "sim"= null;
      "imei" =null;
    }

Used Jackson version is 2.4.1

What can I do about this?

Priyantha
  • 4,839
  • 6
  • 26
  • 46

5 Answers5

2

If the version of SpringBoot is less than 1.3, it can only be handled programmatically

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Response {
    ///~
}

Spring boot can be configured directly from 1.3 in the application.properties file

spring.jackson.default-property-inclusion=non_null

Official documentation for the jacksong configuration

you can use @JsonInclude(JsonInclude.Include.NON_NULL) on sim and imei, Not on the whole class

public class Response{

private String mobileNo;
private String contractId;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String sim;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String imei;

public Response(String mobileNo, String contractId){
    this.mobileNo = mobileNo;
    this.contractId = contractId;

}

public Response(String mobileNo, String contractId, String sim,
        String imei, String identificationType) {
    this.mobileNo = mobileNo;
    this.contractId = contractId;
    this.sim = sim;
    this.imei = imei;
    this.identificationType = identificationType;
}
Destiny.Wang
  • 169
  • 8
  • @Dev4World what r u using? `Gson` or `Jackson` – Destiny.Wang Jul 31 '18 at 06:55
  • @Dev4World you can use `@JsonInclude(JsonInclude.Include.NON_NULL)` on `sim` and `imei`, Not on the whole class – Destiny.Wang Jul 31 '18 at 07:03
  • When I use like this if some values null in the else statement those values remove from the response. But I need those null values with else statement response. Please check my updated question. – Priyantha Jul 31 '18 at 07:24
1

For jackson serializers:

You can use annotation over your class, to skip serializing null values:

@JsonInclude(Include.NON_NULL)
public class Response{...}

Or add a parameter to your ObjectMapper configuration:

mapper.setSerializationInclusion(Include.NON_NULL);

This may be a duplicate.

UPDATE:

You can also annotate properties.

@JsonInclude(JsonInclude.Include.NON_NULL)
private String sim;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String imei;

This way other properties will serialize null values, but those two will not be serialized with null value.

Beri
  • 11,470
  • 4
  • 35
  • 57
1

If you use jackson then add this:

@JsonInclude(JsonInclude.Include.NON_NULL) before field.

public class Response{

private String mobileNo;
private String contractId;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String sim;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String imei;
}
GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39
1

What you ask it's not possible to manage just with serialization.

I suggest to edit Response class, removing the field that don't want send when they are null.

Then create another class that extends Response, that have the other 2 fields.

At this point you can instantiate which one you want based on your condition and return anyway as a Response object.

public class SimpleResponse {
    String mobileNo;
    String contractId;

    .....getters setters
}



public class FullResponse extends SimpleResponse {
    String sim;
    String imei;

    ....getter and setters
}
Frighi
  • 475
  • 4
  • 17
0

Add this annotation just above the getter of sim and imei

 @JsonInclude(Include.NON_NULL)

With Jackson > 1.9.11 and < 2.x use

  @JsonSerialize 

annotation to do that:

   @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

For the version above 2:

 @JsonInclude(JsonInclude.Include.NON_NULL)
Frighi
  • 475
  • 4
  • 17