使用RestTemplate将JSON映射到对象
我编写了货币转换器程序,该程序从
api.fixer.io
读取JSON
,映射对象并创建选定汇率的简单数据集。
我的程序一直工作得很好,直到我停止使用Jackson
解析和映射对象,并将其替换为RestTemplate
。
它可以很好地读取基础货币和日期,但不能读取Rates
子对象。为什么?
我的代码:
Currency
类:
package com.github.gromo13.currencyConverter.model.util;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Currency {
private String base;
private String date;
private Rates rates;
public Currency() {
}
public String getBase() {
return this.base;
}
public void setBase(String base) {
this.base = base;
}
public String getDate() {
return this.date;
}
public void setDate(String date) {
this.date = date;
}
public Rates getRates() {
return this.rates;
}
public void setRates(Rates rates) {
this.rates = rates;
}
public double getRate(String currencyCode) {
return rates.getRate(currencyCode);
}
}
Rates
类:
package com.github.gromo13.currencyConverter.model.util;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Rates {
private double eur;
private double pln;
private double usd;
public double getEur() {
return this.eur;
}
public void setEur(double eur) {
this.eur = eur;
}
public double getPln() {
return this.pln;
}
public void setPln(double pln) {
this.pln = pln;
}
public double getUsd() {
return this.usd;
}
public void setUsd(double usd) {
this.usd = usd;
}
public double getRate(String currencyCode) {
currencyCode = currencyCode.toUpperCase();
switch (currencyCode) {
case "EUR":
return this.eur;
case "PLN":
return this.pln;
case "USD":
return this.usd;
default:
return 1;
}
}
}
Repository
必须使用RestTemplate
映射Currency
对象的类:
package com.github.gromo13.currencyConverter.repository;
import com.github.gromo13.currencyConverter.model.util.Currency;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;
@Repository
public class FixerIoCurrencyRepository implements CurrencyRepository {
@Autowired
private RestTemplate restTemplate;
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@Override
public Currency getCurrency(String currencyCode) {
Currency currency = restTemplate.getForObject("http://api.fixer.io/latest?base={currencyCode}", Currency.class, currencyCode);
return currency;
}
}
Sample JSON data I am trying to parse and map
我正在使用这个Currency
对象准备DataSet
对象和<currencyName, rate>
的简单映射,并将其打印到视图中的表格中。一切正常,我只是每次都收到0
作为每种货币的汇率。
解决方案
问题在于,在Rates
中,类属性为小写,而在JSON数据中为大写。
@Configuration
类中定义RestTemplate
以使用接受不区分大小写的属性。这样,您就不必更改Rates
属性的格式。
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
return mapper;
}
@Bean
public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper());
return converter;
}
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(0, mappingJacksonHttpMessageConverter());
return restTemplate;
}
相关文章