Michael Hegner
Michael Hegner

Reputation: 5823

Spring Integration http:outbound-gateway using json subparts

I hope the title is clear. I am using Spring Integration and I would like to do followings. I would like to consume the following api to enrich a payload: https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC&tsyms=USD

As you see it has a structure like RAW/BTC/USD/ and then attributes. I am only interested in a few attribute values.

My outbound-gateway is as following and works so far:

<int:chain input-channel="internal.cryptocompare.coin.market.enrich.channel">
    <int-http:outbound-gateway id="cryptocompareHttpGateway.marketData" 
        url="https://min-api.cryptocompare.com/data/pricemultifull?fsyms={fsym}&amp;tsyms={tsyms}"
        http-method="GET" 
        reply-timeout="10000" 
        charset="UTF-8"
    >
        <int-http:uri-variable name="fsym" expression="payload.symbol" />
        <int-http:uri-variable name="tsyms" expression="'USD'" />
    </int-http:outbound-gateway>
</int:chain>

I get then a ResponseEntity which I would like to get the attributes e.g. PRICE.

Instead having a ResponseEntity I would also have a value object what I could use too, but the fields were not enriched:

<int:enricher id="coinMarketEnricher"
    input-channel="internal.cryptocompare.coin.price.enriched.income.channel"
    request-channel="internal.cryptocompare.coin.market.enrich.channel"
    output-channel="cryptocompare.income.channel"
    error-channel="cryptocompare.error.channel"
>
    <int:property name="volume24hUSD" expression="payload.volume24hUSD"/>
    <int:property name="marketCapUSD" expression="payload.marketCapUSD"/>
    <int:property name="availableSupply" expression="payload.availableSupply"/>
    <int:property name="changePercent24h" expression="payload.changePercent24h"/>
    <int:property name="lastUpdateEpoch" expression="payload.lastUpdateEpoch"/>
    <int:property name="marketName" expression="payload.marketName"/>
</int:enricher>

<int:channel id="internal.cryptocompare.coin.market.enrich.channel" />

<int:chain input-channel="internal.cryptocompare.coin.market.enrich.channel">
    <int-http:outbound-gateway id="cryptocompareHttpGateway.marketData" 
        url="https://min-api.cryptocompare.com/data/pricemultifull?fsyms={fsym}&amp;tsyms={tsyms}"
        http-method="GET" 
        reply-timeout="10000" 
        charset="UTF-8"
        expected-response-type="net.hemisoft.ccm.porter.cryptocompare.Coin"
    >
        <int-http:uri-variable name="fsym" expression="payload.symbol" />
        <int-http:uri-variable name="tsyms" expression="'USD'" />
    </int-http:outbound-gateway>
</int:chain>

@ToString
class Coin {
    @JsonProperty("Id")                 String  coinId
    @JsonProperty("CoinName")           String  name
    @JsonProperty("Name")               String  symbol
    @JsonProperty("SortOrder")          Integer rank
    @JsonProperty("USD")                Double  priceUSD
    @JsonProperty("BTC")                Double  priceBTC
    @JsonProperty("VOLUME24HOUR")       Double  volume24hUSD
    @JsonProperty("MKTCAP")             Double  marketCapUSD
    @JsonProperty("SUPPLY")             Double  availableSupply
    @JsonProperty("CHANGEPCT24HOUR")    Double  changePercent24h
    @JsonProperty("LASTUPDATE")         Long    lastUpdateEpoch
    @JsonProperty("LASTMARKET")         String  marketName
}

I would prefer using Coin_Class over Response-Entity. Thank you very much for your input.

Upvotes: 1

Views: 704

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121272

For that purpose you need to add this:

expected-response-type="Coin"

into the <int-http:outbound-gateway> definition: https://docs.spring.io/spring-integration/docs/5.0.4.RELEASE/reference/html/http.html#_httprequestexecutingmessagehandler

Further to the note above regarding empty response bodies, if a response does contain a body, you must provide an appropriate expected-response-type attribute or, again, you will simply receive a ResponseEntity with no body.

UPDATE

Since a JSON you get doesn't fit your Coin model directly you can do it like:

  • Send new Coin to the internal.cryptocompare.coin.price.enriched.income.channel
  • The request-channel="internal.cryptocompare.coin.market.enrich.channel" will perform an HTTP request
  • There instead of expected-response-type="net.hemisoft.ccm.porter.cryptocompare.Coin", you should use expected-response-type="java.lang.String"
  • Then in those <int:property> for mapping you need to perform #jsonPath() to extract required values, e.g. <int:property name="volume24hUSD" expression="#jsonPath(payload, '$..VOLUME24HOUR[1]')"/>

See more info here: https://docs.spring.io/spring-integration/docs/5.0.4.RELEASE/reference/html/spel.html and here: https://github.com/json-path/JsonPath

Upvotes: 1

Related Questions