如何在java中传递不同的构造函数作为响应

如何在java中传递不同的构造函数作为响应,java,Java,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;

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;
   }
}
当if语句调用时,返回响应为

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

但我想得到如下回应:

调用if语句时

{ "mobileNo" = "777";
  "contractId" = "4545";
  "sim"= "sim";
  "imei" = "imei";
}
需要删除其他两个值

{ "mobileNo" = "777";
  "contractId" = "4545";
}
如果construcd和mobileNo为null,则输出应为

{ "mobileNo" = null;
  "contractId" = null;
}
调用else语句时

{ "mobileNo" = "777";
  "contractId" = "4545";
  "sim"= "sim";
  "imei" = "imei";
}
如果所有值都为空

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

使用的Jackson版本是2.4.1


对此我能做些什么?

对于jackson序列化程序:

您可以在类上使用注释,以便:

或将参数添加到ObjectMapper配置中:

mapper.setSerializationInclusion(Include.NON_NULL);
这可能是一个错误

更新:

您还可以添加注释


通过这种方式,其他属性将序列化null值,但这两个属性不会用null值序列化。

对于jackson序列化程序:

您可以在类上使用注释,以便:

或将参数添加到ObjectMapper配置中:

mapper.setSerializationInclusion(Include.NON_NULL);
这可能是一个错误

更新:

您还可以添加注释


这样,其他属性将序列化空值,但这两个属性不会序列化为空值。

如果使用
jackson
,请添加以下内容:

@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;
}

如果使用
jackson
,则添加以下内容:

@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;
}

在sim和imei的getter上方添加此注释

 @JsonInclude(Include.NON_NULL)
在Jackson>1.9.11和<2.x的情况下使用

  @JsonSerialize 
要做到这一点:

   @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
对于以上版本2:

 @JsonInclude(JsonInclude.Include.NON_NULL)

在sim和imei的getter上方添加此注释

 @JsonInclude(Include.NON_NULL)
在Jackson>1.9.11和<2.x的情况下使用

  @JsonSerialize 
要做到这一点:

   @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
对于以上版本2:

 @JsonInclude(JsonInclude.Include.NON_NULL)

如果SpringBoot的版本低于1.3,则只能通过编程方式进行处理

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Response {
    ///~
}
可以直接从
application.properties
文件中的1.3配置Spring引导

spring.jackson.default-property-inclusion=non_null

您可以在sim卡和imei上使用
@JsonInclude(JsonInclude.Include.NON_NULL)
,而不是在整个类上

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;
}

如果SpringBoot的版本低于1.3,则只能通过编程方式进行处理

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Response {
    ///~
}
可以直接从
application.properties
文件中的1.3配置Spring引导

spring.jackson.default-property-inclusion=non_null

您可以在sim卡和imei上使用
@JsonInclude(JsonInclude.Include.NON_NULL)
,而不是在整个类上

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;
}

您所要求的内容不可能仅通过序列化进行管理

我建议编辑Response类,删除不希望在为null时发送的字段

然后创建另一个扩展响应的类,该类包含其他两个字段

此时,您可以根据您的条件实例化所需的对象,并以响应对象的形式返回

public class SimpleResponse {
    String mobileNo;
    String contractId;

    .....getters setters
}



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

    ....getter and setters
}

您所要求的内容不可能仅通过序列化进行管理

我建议编辑Response类,删除不希望在为null时发送的字段

然后创建另一个扩展响应的类,该类包含其他两个字段

此时,您可以根据您的条件实例化所需的对象,并以响应对象的形式返回

public class SimpleResponse {
    String mobileNo;
    String contractId;

    .....getters setters
}



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

    ....getter and setters
}


您使用的是
gson
?哪个库用于序列化“但我希望在调用if语句时得到下面的响应。”=>真正的问题是:为什么?您使用的是
gson
?哪个库用于序列化“但我希望在调用if语句时得到下面的响应。”=>真正的问题是:为什么?@Dev4World您使用什么
Gson
Jackson
@Dev4World您可以在
sim
imei
上使用
@JsonInclude(JsonInclude.Include.NON_NULL)
而不是在整个类上,当我这样使用时,如果else语句中的某些值为NULL,这些值将从响应中删除。但是我需要那些带有else语句响应的空值。请检查我更新的问题。@Dev4World您使用的是什么
Gson
Jackson
@Dev4World您可以在
sim
imei
上使用
@JsonInclude(JsonInclude.Include.NON_NULL)
而不是在整个类上,当我这样使用时,如果else语句中的某些值为NULL,这些值将从响应中删除。但是我需要那些带有else语句响应的空值。请检查我更新的问题。再次检查我的答案可能您有更新版本的Jackson,请在要忽略的文件的获取者上方放置注释。Jackson版本为2.4.1请检查此添加的答案。我尝试了更新的答案。但问题是一样的。我还更新了我的问题。再次检查我的回答也许你有更新版本的Jackson,在要忽略的文件的getter上方放置注释Jackson版本为2.4.1检查此添加的答案partI尝试了更新的答案。但问题是一样的。我还更新了我的问题。