如何转换时忽略 getter,但是 setter 依然需要转换

通过注解@JsonProperty并且传参JsonProperty.Access.WRITE_ONLY

例如下面

public class TestJson {

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String s1;
    private String s2;


    public String getS1() {
        return s1;
    }

    public void setS1(String s1) {
        this.s1 = s1;
    }

    public String getS2() {
        return s2;
    }

    public void setS2(String s2) {
        this.s2 = s2;
    }
}

或者下面这样也可以

public class TestJson {
    private String s1;
    private String s2;


    @JsonIgnore
    public String getS1() {
        return s1;
    }

    @JsonProperty
    public void setS1(String s1) {
        this.s1 = s1;
    }

    public String getS2() {
        return s2;
    }

    public void setS2(String s2) {
        this.s2 = s2;
    }
}

黑不溜秋
203 声望2 粉丝