Gro*_*omo 1 java spring json spring-mvc resttemplate
我编写了货币转换器程序,它JSON从api.fixer.io、映射对象中读取并创建选定汇率的简单数据集。我的程序运行良好,直到我停止使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Repository必须Currency使用RestTemplate以下方法映射对象的类:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用此Currency对象准备DataSet带有简单地图的对象,<currencyName, rate>并将其打印在视图中的表格中。一切正常,我每次都收到0每种货币的汇率。
问题是Rates类中的属性是小写的,而 JSON 数据是大写的。
您可以在@Configuration类中定义RestTemplate使用ObjectMapper不区分大小写的接受属性。这样,您就不必更改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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8207 次 |
| 最近记录: |