1

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Taha LAGHZALI
  • 464
  • 2
  • 14

1 Answers1

1

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.

Moemen
  • 364
  • 3
  • 11
  • Actually, I removed the cache option but I should activate it after. – Taha LAGHZALI Jul 26 '22 at 11:02
  • Actually, I removed the cache option but I should activate it after when I'll find another solution. Concerning setters methods , I can't remove one of them. I need one (public void setZipCode(String[] zipCode)) during deserialization of a json , and I need the second (public void setZipCode(String zipCode)) for mapping zipCode property . – Taha LAGHZALI Jul 26 '22 at 11:10
  • You can try to keep the setter which accepts array/list of strings, and replace `private String zipCode;` with `private List zipCodes;` and in the service/repository level retrieve the first zipCode if you would like. – Moemen Jul 26 '22 at 11:33