Spring Integration Http Outbound Gateway提取JSON子字段实现Payload增强
Hey there! 😊 Since you’ve already got your Spring Integration HTTP Outbound Gateway working smoothly with the CryptoCompare API, extracting specific nested JSON fields from that RAW/BTC/USD structure is straightforward. Let’s cover the most common, practical approaches you can use right away:
1. Use JsonPath Transformer (Quick & Declarative)
Spring Integration has built-in support for JsonPath, which lets you extract exactly the fields you need without writing custom Java code. You can either replace the payload with a simplified map of your target fields, or use an enricher to add these fields to your existing payload.
Example: Replace Payload with Extracted Fields
This transforms the full JSON response into a compact map containing only your desired fields:
<int:chain input-channel="internal.cryptocompare.coin.market.enrich.channel"> <!-- Your existing HTTP Outbound Gateway --> <int-http:outbound-gateway url="https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC&tsyms=USD" http-method="GET" expected-response-type="java.lang.String"/> <!-- Extract specific fields using JsonPath --> <int:transformer> <int:json-path-expression> { "btcUsdPrice": "$.RAW.BTC.USD.PRICE", "lastUpdateTimestamp": "$.RAW.BTC.USD.LASTUPDATE", "marketCap": "$.RAW.BTC.USD.MKTCAP" } </int:json-path-expression> </int:transformer> </int:chain>
Example: Enrich Your Existing Payload
If you need to keep your original payload and just add the crypto data to it (the "enhance" use case), use an <int:enricher> with JsonPath expressions:
<int:chain input-channel="internal.cryptocompare.coin.market.enrich.channel"> <!-- First, store your original payload in a header so we don't lose it --> <int:header-enricher> <int:header name="originalPayload" expression="payload"/> </int:header-enricher> <!-- Make the API call (payload becomes the JSON response) --> <int-http:outbound-gateway url="https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC&tsyms=USD" http-method="GET" expected-response-type="java.lang.String"/> <!-- Enrich the original payload with extracted crypto fields --> <int:enricher payload-expression="headers['originalPayload']"> <int:property name="btcUsdPrice" expression="#jsonPath(payload, '$.RAW.BTC.USD.PRICE')"/> <int:property name="lastUpdated" expression="#jsonPath(payload, '$.RAW.BTC.USD.LASTUPDATE')"/> </int:enricher> </int:chain>
2. Custom Java Transformer (For Complex Logic)
If you need more control—like type conversion, validation, or mapping to a custom POJO—write a simple Java component to handle the extraction:
Step 1: Create the Extractor Class
@Component("cryptoFieldExtractor") public class CryptoCompareFieldExtractor { private final ObjectMapper objectMapper = new ObjectMapper(); public CryptoPriceData extractRelevantFields(String jsonResponse) throws IOException { JsonNode root = objectMapper.readTree(jsonResponse); JsonNode btcUsdNode = root.path("RAW").path("BTC").path("USD"); CryptoPriceData data = new CryptoPriceData(); data.setPrice(btcUsdNode.path("PRICE").asDouble()); data.setLastUpdate(btcUsdNode.path("LASTUPDATE").asLong()); data.setVolume24h(btcUsdNode.path("VOLUME24HOUR").asDouble()); return data; } // POJO to hold your extracted data public static class CryptoPriceData { private double price; private long lastUpdate; private double volume24h; // Getters and setters } }
Step 2: Configure the Transformer in XML
<int:chain input-channel="internal.cryptocompare.coin.market.enrich.channel"> <int-http:outbound-gateway url="https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC&tsyms=USD" http-method="GET" expected-response-type="java.lang.String"/> <!-- Use your custom extractor --> <int:transformer ref="cryptoFieldExtractor" method="extractRelevantFields"/> </int:chain>
Important Note
Don’t forget to include the Spring Integration JSON dependency in your project (if you haven’t already):
<!-- Maven example --> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-json</artifactId> <!-- Match version to your Spring Integration core version --> </dependency>
All these approaches work seamlessly with your existing chain setup—pick the one that best fits your needs (declarative vs. custom logic)!
内容的提问来源于stack exchange,提问作者Michael Hegner

