【问题标题】:Android retrofit2 sending json via jackson [duplicate]Android retrofit2通过杰克逊发送json [重复]
【发布时间】:2016-07-20 07:56:11
【问题描述】:

我有一个User 类,如下所示:

public class User implements Serializable {

    @JsonProperty("image")
    private UserImage image;

    @JsonProperty("email")
    private String email;

    @JsonProperty("mobilePhone")
    private String mobilePhone;

    @JsonProperty("firstname")
    private String firstName;

    @JsonProperty("lastname")
    private String lastName;

    @JsonProperty("codeCreatedAt")
    private String codeCreatedAt;

    @JsonProperty("accountSettings")
    private AccountSettings accountSettings;

    @JsonProperty("status")
    private String status;

    @JsonProperty("created")
    private String created;

    @JsonProperty("id")
    private String id;

    public User(String lastName, String firstName, String mobilePhone) {
        this.lastName = lastName;
        this.firstName = firstName;
        this.mobilePhone = mobilePhone;
    }

    public User() {
    }

    public User(UserImage image, String email, String mobilePhone, String firstName, String lastName, String codeCreatedAt, AccountSettings accountSettings, String status, String created, String id) {
        this.image = image;
        this.email = email;
        this.mobilePhone = mobilePhone;
        this.firstName = firstName;
        this.lastName = lastName;
        this.codeCreatedAt = codeCreatedAt;
        this.accountSettings = accountSettings;
        this.status = status;
        this.created = created;
        this.id = id;
    }
}

我正在尝试发送一个 Json,例如:{"mobilePhone":"123456789","lastname":"test","firstname":"test"}

我创建了一个新用户,使用带有 3 个参数的构造函数,但它使 JSON 基于带有所有参数的构造函数。我需要更改什么以获得所需的 JSON?

【问题讨论】:

    标签: java android json jackson


    【解决方案1】:

    @JsonInclude(JsonInclude.Include.NON_NULL) 添加到班级

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class User implements Serializable {...}
    

    【讨论】:

      猜你喜欢
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多