![](/img/trans.png)
[英]Spring RestTemplate getForObject Map<String, POJO> class mapping
[英]RestTemplate mapping to Map instead of fields
我編寫了一個簡單的貨幣轉換器,該轉換器從Web服務讀取JSON,並以選定的比率在網站上准備表格。
到目前為止,我的Rates類具有每個比率的字段,但是我決定將其更改為Map
。 我使用Map
重寫了整個類,但RestTemplate
無法將JSON
數據映射到我的HashMap。 整個字段均視為null
。 如何重新配置RestTemplate
或ObjectMapper
以啟用將JSON
映射到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
費率類(Currency類中的字段):package com.github.gromo13.currencyConverter.model; 導入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配置:包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;
}
}
它不會像現在那樣發揮作用。 您不需要Rates
類,可以完全擺脫它並使用:
private Map<String, Double> rates
在貨幣類中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.