我写过简单的货币转换器,它从网络服务中读取JSON,并在网站上以选定的比率准备表格。
到目前为止,我的费率类为每个比率都有字段,但我决定将其更改为Map
。我使用Map
重写了整个课程,但RestTemplate
无法将JSON
数据映射到我的HashMap。整个字段被视为null
。
如何重新配置RestTemplate
或ObjectMapper
以将JSON
映射到Map
?
Example JSON string that I am trying to map
我用来读取JSON
并将其映射到对象上的存储库类:
package com.github.gromo13.currencyConverter.repository;
import com.github.gromo13.currencyConverter.model.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;
}
}
货币类我使用JSON数据进行映射: package com.github.gromo13.currencyConverter.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Currency {
private String base;
private String date;
private Rates rates;
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;
}
}
使用我无法映射的Map
的费率类(货币类中的字段):
package com.github.gromo13.currencyConverter.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.HashMap;
import java.util.Map;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Rates {
private Map<String, Double> rates = new HashMap<>();
public void clear() {
rates.clear();
}
public void setRate(String currencyCode, double rate) {
rates.put(currencyCode.toUpperCase(), rate);
}
public double getRate(String currencyCode) {
return rates.get(currencyCode.toUpperCase());
}
}
我的实际RestTemplate配置: package com.github.gromo13.currencyConverter.config;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
@Configuration
public class Config {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(0, mappingJackson2HttpMessageConverter());
return restTemplate;
}
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
return mapper;
}
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper());
return converter;
}
}
答案 0 :(得分:1)
它不会像现在这样运作。你不需要private Map<String, Double> rates
类,你可以完全摆脱它并使用:
if (someParam1 != 0)
{
myQuery = myQuery.Where(q => q.SomeField1 == someParam1)
}
if (someParam2 != 0)
{
myQuery = myQuery.Where(q => q.SomeField2 == someParam2)
}
在货币类中。