Home > Mobile >  Could not read JSON: Conflicting setter definitions for property
Could not read JSON: Conflicting setter definitions for property

Time:07-26

When I call the API for the first time, I receive a JSON response without any error.

When I call the API for the second time, I receive:

Could not read JSON: Conflicting setter definitions for property "zipCode": com.sfr.cacheaddress.api.model.Address#setZipCode(1 params) vs com.sfr.cacheaddress.api.model.Address#setZipCode(1 params)
 at [Source: (byte[])"{"@class":"com.sfr.cacheaddress.api.model.MetaAddress","address":{"id":"3525622624","streetNumber":5,"streetNumberComplement":"A","streetType":"RUE","streetName":"RUE DES PERRIERES","streetNumberAndName":"5 A RUE DES PERRIERES","zipCode":"35800","city":"ST BRIAC SUR MER","address":"5 A RUE DES PERRIERES,
    35800 ST BRIAC SUR MER","inseeCode":"35256","enabled":false,"codeRivoli":"0611","matriculeVoie":"00676767"},"complementCount":1}"; line: 1, column: 11
]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "zipCode": com.sfr.cacheaddress.api.model.Address#setZipCode(1 params) vs com.sfr.cacheaddress.api.model.Address#setZipCode(1 params)
 at [Source: (byte[])"{"@class":"com.sfr.cacheaddress.api.model.MetaAddress","address":{"id":"3525622624","streetNumber":5,"streetNumberComplement":"A","streetType":"RUE","streetName":"RUE DES PERRIERES","streetNumberAndName":"5 A RUE DES PERRIERES","zipCode":"35800","city":"ST BRIAC SUR MER","address":"5 A RUE DES PERRIERES,
    35800 ST BRIAC SUR MER","inseeCode":"35256","enabled":false,"codeRivoli":"0611","matriculeVoie":"00676767"},"complementCount":1}"; line: 1, column: 11

The Get API:

@GetMapping("streetNumber")
public MetaAddress getAddressByIdRivoliStreetNumber(String code) {
        return addressService.findByRivoliStreetNumber(code);
}

MetaAddress.java:

@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class MetaAddress {
    Address address;
    long complementCount;
}

Address.java:

@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public final class Address implements Serializable {

    private String id;
    private String zipCode;
    private String city;

    //@JsonIgnore
    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    @JsonSetter
    public void setZipCode(String[] zipCode) {
        this.zipCode = zipCode[0];
    }
}

During deserializing, the second setZipCode(String[] zipCode) is needed (which contains @JsonSetter), I tried to add @JsonIgnore to first method public void setZipCode(String zipCode) but the issue persists.

Precision: Other APIs which use Adress class (not MetaAddres) work fine. The issue happens just when I call the API that uses MetaAddress object

Anyone encountered the same problem please?

CodePudding user response:

One solution is to remove the cache. Another better solution is to remove the conflicting setter by removing one of them or removing both as you are already using @Data annotation.

If you expect different zipCode formats from the json returned by addressService.findByRivoliStreetNumber(code) such as string and array of strings, then you can deal with both formates as different properties.

If you have a special reason to do this, please mention it. Maybe there is a better design handing of this case.

  • Related