#Single Entity Class for Two Responses Java Spring

6 messages · Page 1 of 1 (latest)

weak coral
#

Below are two responses from 2 different APIs. The structure of the responses is the same, but the names of the two variables are different. How do I create a single entity class where I can save the responses?

First API:
"""
{
"success": true,
"timestamp": 1519296206,
"base": "USD",
"rates": {
"GBP": 0.72007,
"JPY": 107.346001,
"EUR": 0.813399,
}
}
"""

Second API:
"""
{
"success": true,
"timestamp": 1432400348,
"source": "USD",
"quotes": {
"USDAUD": 1.278342,
"USDEUR": 1.278342,
"USDGBP": 0.908019,
"USDPLN": 3.731504
}
}
"""

My service method for first API:

@Override
    public FixerCurrency getEuro() {
        httpHeaders.setAccept(List.of(MediaType.APPLICATION_JSON));
        HttpEntity<String> entity = new HttpEntity<>(httpHeaders);

        ResponseEntity<FixerCurrency> response = restTemplate.exchange(fixerApi, HttpMethod.GET, entity, FixerCurrency.class);

        return response.getBody();
    }

My service method for second API:

    @Override
    public CurrencylayerCurrency getDollar() {
        httpHeaders.setAccept(List.of(MediaType.APPLICATION_JSON));
        HttpEntity<String> entity = new HttpEntity<>(httpHeaders);

        ResponseEntity<CurrencylayerCurrency> response = restTemplate.exchange(currencylayerApi, HttpMethod.GET, entity, CurrencylayerCurrency.class);

        return response.getBody();
    }
broken stagBOT
#

This post has been reserved for your question.

Hey @weak coral! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

obtuse eagle
#

Create a normalized class and then map the results to that. You can code it by hand or use MapStruct.

gilded pivot
#

Jackson has @JsonAlias

#

Probably what you want