json - Spring 集成 http :outbound-gateway using json subparts

标签 json spring http spring-integration

我希望标题清楚。我正在使用 Spring Integration,我想执行以下操作。我想使用以下 api 来丰富有效负载:https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC&tsyms=USD

如您所见,它具有类似于 RAW/BTC/USD/的结构,然后是属性。我只对几个属性值感兴趣。

我的出站网关如下所示并且目前有效:

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

然后我得到一个 ResponseEntity,我想从中获取属性,例如价格。

代替 ResponseEntity 我也会有一个我也可以使用的值对象,但字段没有得到丰富:

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

我更喜欢使用 Coin_Class 而不是 Response-Entity。 非常感谢您的意见。

最佳答案

为此,您需要添加以下内容:

expected-response-type="Coin"

进入<int-http:outbound-gateway>定义: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.

更新

由于您获得的 JSON 不适合您的 Coin直接建模你可以这样做:

  • 发送new Coininternal.cryptocompare.coin.price.enriched.income.channel
  • request-channel="internal.cryptocompare.coin.market.enrich.channel"将执行 HTTP 请求
  • 而不是expected-response-type="net.hemisoft.ccm.porter.cryptocompare.Coin" , 你应该使用 expected-response-type="java.lang.String"
  • 然后在那些<int:property>对于映射,您需要执行 #jsonPath()提取所需的值,例如<int:property name="volume24hUSD" expression="#jsonPath(payload, '$..VOLUME24HOUR[1]')"/>

在此处查看更多信息:https://docs.spring.io/spring-integration/docs/5.0.4.RELEASE/reference/html/spel.html在这里:https://github.com/json-path/JsonPath

关于json - Spring 集成 http :outbound-gateway using json subparts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50142620/

相关文章:

javascript - 访问奇怪的对象

java - jparepository 错误创建 entitymanagerfactory

Spring 启动: Is this correct way to save a new entry which has ManyToOne relationship?

java.lang.ClassNotFoundException : com. springhibernatemvc.dao.PersonDAOImpl

regex - Apache http + mod-rewrite 到 Tomcat 以从 url 丢失应用程序子文件夹

python - 无法将 mysql 中的特殊字符存储到 json 文件中 - Python

javascript - 在按钮上返回 id 以显示特定的 json react js

PHP MySQL JSON 更新

http - 从 C# 移植到 F# 时出现 System.Net.WebException

c - 从 libevent 中的 HTTP 服务器响应中获取所有 HTTP header