gpt4 book ai didi

java - 映射嵌套枚举时获取 JsonMappingException

转载 作者:行者123 更新时间:2023-11-30 04:20:28 26 4
gpt4 key购买 nike

我使用spring RestTemplate并将json响应转换为pojo。但得到一个 JsonMappingException

POJO

@Root
public class Account {

@Element
private int id;
@Element
private int customerId;
@Element
private AccountType type;
@Element
private BigDecimal balance;

public int getId() {
return id;
}

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

public int getCustomerId() {
return customerId;
}

public void setCustomerId(int customerId) {
this.customerId = customerId;
}

public AccountType getType() {
return type;
}

public int getIntType() {
return type.ordinal();
}

public void setType(AccountType type) {
this.type = type;
}

public void setType(int type) {
this.type = AccountType.values()[type];
}

public BigDecimal getBalance() {
return balance;
}

public void setBalance(BigDecimal balance) {
this.balance = balance;
}

public BigDecimal getAvailableBalance() {
return balance.signum() < 0 ? new BigDecimal(0) : balance;
}

public void credit(BigDecimal amount) {
balance = balance.add(amount);
}

public void debit(BigDecimal amount) {
balance = balance.subtract(amount);
}






public static enum AccountType {
CHECKING(false), SAVINGS(false), LOAN(true);

private boolean internal;

private AccountType(boolean internal) {
this.internal = internal;
}

public boolean isInternal() {
return internal;
}
}
}

休息客户端

RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
MediaType mediaType = new MediaType("application", "json", Charset.forName("UTF-8"));
supportedMediaTypes.add(mediaType);
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
jacksonConverter.setSupportedMediaTypes(supportedMediaTypes);
messageConverters.add(jacksonConverter);
restTemplate.setMessageConverters(messageConverters);

Account [] accounts = restTemplate.getForObject(url, new Account[0].getClass());`

异常

 Caused by: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Conflicting setter definitions for property "type": com.entity.Account#setType(1 params) vs com.entity.Account#setType(1 params); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "type": com.entity.Account#setType(1 params) vs com.entity.Account#setType(1 params)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:126)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)`

最佳答案

问题是您有 2 个名为 setType 的方法,而转换器不知道使用哪一个。将注释 @JsonIgnore 放在您想要用于反序列化的方法上,这应该可以解决问题。

关于java - 映射嵌套枚举时获取 JsonMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17179684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com