[英]Could not read JSON: Conflicting setter definitions for property
When I call the API for the first time, I receive a JSON response without any error.当我第一次调用 API 时,我收到 JSON 响应,没有任何错误。
When I call the API for the second time, I receive:当我第二次调用 API 时,我收到:
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:获取 API:
@GetMapping("streetNumber") public MetaAddress getAddressByIdRivoliStreetNumber(String code) { return addressService.findByRivoliStreetNumber(code); }
MetaAddress.java:元地址.java:
@Data @NoArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) public class MetaAddress { Address address; long complementCount; }
Address.java:地址.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.在反序列化期间,需要第二个
setZipCode(String[] zipCode)
(其中包含@JsonSetter
),我尝试将@JsonIgnore
添加到第一个方法public void setZipCode(String zipCode)
但问题仍然存在。
Precision: Other APIs which use Adress
class (not MetaAddres
) work fine.精度:使用
Adress
class (不是MetaAddres
)的其他 API 工作正常。 The issue happens just when I call the API that uses MetaAddress
object问题发生在我调用使用
MetaAddress
object 的 API
Anyone encountered the same problem please?请问有人遇到同样的问题吗?
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.另一个更好的解决方案是通过删除其中一个或同时删除两个设置器来删除冲突的设置器,因为您已经在使用
@Data
注释。
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.如果您期望与
addressService.findByRivoliStreetNumber(code)
返回的 json 不同的邮政编码格式,例如 string 和字符串数组,那么您可以将两种甲酸盐作为不同的属性处理。
If you have a special reason to do this, please mention it.如果您有特殊原因这样做,请提及。 Maybe there is a better design handing of this case.
也许这种情况下有更好的设计处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.