FasterXML / jackson-databind

General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)
Apache License 2.0
3.52k stars 1.38k forks source link

the serialize json result has one more property which do not declared #3323

Open mongo-o opened 2 years ago

mongo-o commented 2 years ago

Describe the bug when i serialize the DomainGroupDto instance , the always get one more property “irightGroupName” which do not declared。 the result is : image

The DomainGroupDto define as below :

public class DomainGroupDto implements Serializable {
    private static final long serialVersionUID = 1L;

    @JsonProperty("id")
    private String id = null;

    @JsonProperty("i_right_group_name")
    private String iRightGroupName = null;

    public DomainGroupDto id(String id) {
        this.id = id;
        return this;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public DomainGroupDto iRightGroupName(String iRightGroupName) {
        this.iRightGroupName = iRightGroupName;
        return this;
    }

    /**
     * Get iRightGroupName
     * 
     * @return iRightGroupName
     **/
    @ApiModelProperty(value = "")

    public String getIRightGroupName() {
        return iRightGroupName;
    }

    public void setIRightGroupName(String iRightGroupName) {
        this.iRightGroupName = iRightGroupName;
    }

    @Override
    public boolean equals(java.lang.Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        DomainGroupDto domainGroupDto = (DomainGroupDto) o;
        return Objects.equals(this.id, domainGroupDto.id)
                && Objects.equals(this.iRightGroupName, domainGroupDto.iRightGroupName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, iRightGroupName);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("class DomainGroupDto {\n");

        sb.append("    id: ").append(toIndentedString(id)).append("\n");
        sb.append("    iRightGroupName: ").append(toIndentedString(iRightGroupName)).append("\n");
        sb.append("}");
        return sb.toString();
    }

    /**
     * Convert the given object to string with each line indented by 4 spaces
     * (except the first line).
     */
    private String toIndentedString(java.lang.Object o) {
        if (o == null) {
            return "null";
        }
        return o.toString().replace("\n", "\n    ");
    }
}

Version information v2.12.3

To Reproduce

Expected behavior

Additional context below is my debug image where the reason is ,the undeclared property is from the method "getIRightGroupName". image

yawkat commented 2 years ago

The problem here is that the getter getIRightGroupName is not being associated with the field iRightGroupName. Jackson's property naming logic thinks the getter is for a property called irightGroupName (lowercase r). You have multiple options here:

cowtowncoder commented 2 years ago

What @yawkat says: your naming convention is non-standard (wrt Bean naming) and Jackson needs some help with annotations.

I would remove annotations from fields and move to getter (and for that one oddly named property, also setter). There is usually no point in annotating fields if you have getters and setters.