我有这个实体;transferRate在数据库中以json格式保存:
@Entity
@Data
public class Currency implements Serializable {
@Id
private String currencyIsoCode;
private String currencyIsoNum;
@Convert(converter = JpaConverterJson.class)
private Map<String, Object> transferRate = new HashMap();
}
和在客户端项目我使用Rest模板来获取货币列表,如
ResponseEntity<List<Currency>> listResponse =
restTemplate.exchange(RestApiConstants.BASE_URL + CurrenciesRestApiConstants.LIST_CURRENCIES,
HttpMethod.GET, null, new ParameterizedTypeReference<>() {
});
currencyList = listResponse.getBody();
但是我得到这个错误
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `double` from Object value (token `JsonToken.START_OBJECT`)
客户端的类是这样的
@Data
public class Currency implements Serializable {
private String currencyIsoCode;
private String currencyIsoNum;
private Map<String, Object> transferRate;
}
我做错了什么
提前谢谢你
您的字段被定义为Map<String, Object>
,因此Jackson尝试将映射值反序列化为Object
。从JSON的角度来看,对象是花括号中的一组属性。这就是错误的来源:
Cannot deserialize value of type `double` from Object value (token `JsonToken.START_OBJECT`)
显然在客户端你发送一个数值,所以你需要定义transferRate
与数字类型,例如:
@Convert(converter = JpaConverterJson.class)
private Map<String, Double> transferRate = new HashMap();
并在客户端使用类似的类型:
private Map<String, Double> transferRate;