How to ignore some variables in models using for retrofit

前端 未结 3 1981
小鲜肉
小鲜肉 2021-01-12 01:38

I am using Retrofit to send and receive requests to my server.

I Have a model like below and I have to send it to my server but some variables in th

3条回答
  •  失恋的感觉
    2021-01-12 01:55

    Use transient keywork for that

    public class SelectedListModel implements Serializable {
    
      @SerializedName("p_id")
      @Expose
      private Long pId;
    
      @SerializedName("p_qty")
      @Expose
      private Double pQty;
    
      //@Expose(serialize = false , deserialize = false)
      private transient String pName; //Have not to send to server
    
      //@Expose(serialize = false , deserialize = false)
      private transient String pPrice; //Have not to send to server
    
      //@Expose(serialize = false , deserialize = false)
      private transient String pImageUrl; //Have not to send to server
    }
    

    and no need to use @Expose(serialize = false , deserialize = false), into those fields which needed to be excluded.


    Read Why does Java have transient fields? and Why use the `transient` keyword in java? for more details.

提交回复
热议问题