Spring Integration :How to send array as a single payload to a jdbc : outbound gateway

StackOverflow https://stackoverflow.com/questions/22897955

  •  28-06-2023
  •  | 
  •  

Domanda

How to send array as a single payload to a jdbc : outbound gateway of Spring Integration ? I am constructing an array of string and sending it to the interface method . However the SQL accepts one parameter ":payload" and it fails with UncategorizedSQLException.

The Sql query of outbound gateway is as below

<int-jdbc:outbound-gateway data-source="dataSource"   request-channel="requestChannel" 
                           query="select XMLMSG from Table where SEQ_ID in (:payload)"                                
                           reply-channel="replyChannel" > 

</int-jdbc:outbound-gateway>

serviceinterface.findBySequenceIds(sequenceIdStringArray);
È stato utile?

Soluzione

Actually to substituteNamedParameters for IN clause the parameter has to be Collection not array. The source code of NamedParameterJdbcTemplate.substituteNamedParameters:

if (value instanceof Collection) {
    Iterator<?> entryIter = ((Collection<?>) value).iterator();
    int k = 0;
        while (entryIter.hasNext()) {
    if (k > 0) {
        actualSql.append(", ");
        }

......
      actualSql.append("?");

So, just construct List from your strings, not array:

Arrays.asList("foo", "bar", "baz");

And your real query will be:

select XMLMSG from Table where SEQ_ID in (?, ?, ?)

Other work the JdbcTemplate will do for you.

UPDATE

Note, by default <int-jdbc:outbound-gateway/> selects only one record. To get rid this limit specify max-rows-per-poll="0".

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top