简体   繁体   中英

Using RestTemplate to map JSON to object

I wrote Currency Converter program that reads JSON from api.fixer.io , maps object and creates simple data set of selected rates. My program was doing its job well until I stopped using Jackson to parse and map object and replaced it with RestTemplate . It reads base currency and date well, but not Rates subobject. Why?

My Code: Currency class:

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 class:

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 class that have to map Currency object using 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;
    }
}

Sample JSON data I am trying to parse and map

I am using this Currency object to prepare DataSet object with a simple map of <currencyName, rate> and print it in a table in view. Everything works fine, I just receive every time 0 as the rate in each currency.

The problem is that in Rates class properties are lower case and in JSON data upper case.

You can define in a @Configuration class your RestTemplate to use an ObjectMapper accepting case insensitive properties. That way, you don't have to change the format of Rates properties.

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM