Home > Net >  Parsing to Object api fields like eMail, bNumber, iKindCd causes null value
Parsing to Object api fields like eMail, bNumber, iKindCd causes null value

Time:10-13

I try to parse reponse from Api to my custom object.

Most fields are parsed correctly, except fields named like: eMail, iKindName, bNumber, uTypeName, iStartDT, iKindCd, uTypeCd (first small, second capital letter)

In fields like that I have null values if I use my custom object ResponseV2. If I use Object type instead of ResponseV2 - fields eMail, iKindName, bNumber, uTypeName, iStartDT, iKindCd, uTypeCd are not null

Whats wrong with that fields (with pattern: first small and second capital letter) in my ReponseV2? Should I use some annotations, like @JsonProperty here?

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class ResultV2 implements Serializable {
    private String country;
    private String federationNumber;
    private String city;
    private String regon;
    private String managerSurname;
    private String countryCd;
    private String pib;
    private String institutionUuid;
    private String lNumber;
    private String siTypeName;
    private String managerName;
    private String ministryNumber;
    private String eMail;
    private String supervisingInstitutionID;
    private String nip;
    private String street;
    private String www;
    private String espAddress;
    private String voivodeship;
    private String id;
    private String iKindName;
    private String federationComposition;
    private String lastRefresh;
    private String postalCd;
    private String bNumber;
    private String panNumber;
    private List<BranchesV2> branches;
    private String krs;
    private String supervisingInstitutionName;
    private String iLiqStartDT;
    private String eunNumber;
    private String uTypeName;
    private String institutionUid;
    private String phone;
    private String iStartDT;
    private String iLiqDT;
    private String name;
    private String iKindCd;
    private String siTypeCd;
    private String yearPib;
    private String uTypeCd;
    private String dataSource;
    private String voivodeshipCode;
    private String status;
    private String statusCode;
}

enter image description here

CodePudding user response:

I have solution: it works if I have

@JsonProperty ("eMail") private String eMail;

instead of

private String eMail;

But why?

CodePudding user response:

Lombok @Data annotation is generating setter method as

public void setEMail(final String eMail) {
    this.eMail = eMail;
}

However json mapper expects the setter method to be with lower case letter e like in example below, so it could not find the method.

public void seteMail(final String eMail) {
    this.eMail = eMail;
}

Just use JsonProperty like @JsonProperty("eMail") on those fields. It would work fine.

Here is a detailed explanation on why it work like this: Why does Jackson 2 not recognize the first capital letter if the leading camel case word is only a single letter long?

  • Related