Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 映射嵌套枚举时获取JsonMappingException_Java_Json_Spring_Pojo_Resttemplate - Fatal编程技术网

Java 映射嵌套枚举时获取JsonMappingException

Java 映射嵌套枚举时获取JsonMappingException,java,json,spring,pojo,resttemplate,Java,Json,Spring,Pojo,Resttemplate,Im使用SpringRESTTemplate并将json响应转换为pojo。但是得到一个JsonMappingException 波乔 问题是您有两个名为setType的方法,而转换器不知道使用哪一个。将注释@JsonIgnore放在您不想用于反序列化的方法上,这样应该可以解决问题 @Root public class Account { @Element private int id; @Element private int customerId; @Element private Acc

Im使用SpringRESTTemplate并将json响应转换为pojo。但是得到一个
JsonMappingException

波乔


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

@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)`