代码之家  ›  专栏  ›  技术社区  ›  Priyantha Lokesh Gupta

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

  •  0
  • Priyantha Lokesh Gupta  · 技术社区  · 6 年前

    响应.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语句调用返回响应为

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

    但我想得到如下的回应,

    调用if语句时

    需要删除其他两个值。

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

    如果compressed和mobileNo为空,则输出应为

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

    调用else语句时

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

    如果所有值都为空

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

    二手杰克逊版本是2.4.1

    我能怎么办?

    5 回复  |  直到 6 年前
        1
  •  1
  •   Frighi    6 年前

    你所要求的不可能仅仅通过序列化来管理。

    我建议编辑Response类,删除当它们为空时不希望发送的字段。

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

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

    public class SimpleResponse {
        String mobileNo;
        String contractId;
    
        .....getters setters
    }
    
    
    
    public class FullResponse extends SimpleResponse {
        String sim;
        String imei;
    
        ....getter and setters
    }
    
        2
  •  2
  •   Destiny.Wang    6 年前

    如果SpringBoot的版本低于1.3,则只能以编程方式处理它

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

    可以直接从 application.properties 文件

    spring.jackson.default-property-inclusion=non_null
    

    Official documentation for the jacksong configuration

    你可以用 @JsonInclude(JsonInclude.Include.NON_NULL) 在sim和imei上,不是在全班上

    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;
    }
    
        3
  •  1
  •   GolamMazid Sajib    6 年前

    如果你使用 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;
    }
    
        4
  •  1
  •   Beri    6 年前

    对于杰克逊系列:

    您可以在类上使用注释,以 skip serializing null values :

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

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

    mapper.setSerializationInclusion(Include.NON_NULL);
    

    这可能是 duplicate .

    更新:

    您也可以注释 properties .

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

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

        5
  •  0
  •   Frighi    6 年前

    在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)