You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Integration JDBC出站网关空结果集处理的Advice问题排查

Spring Integration JDBC出站网关处理空结果集异常问题

尝试使用Spring Integration的JDBC出站网关查询数据,当结果集为空时希望返回空数组而非抛出异常,配置了ExpressionEvaluatingRequestHandlerAdvice但未生效,请求帮忙排查原因。

配置代码

<beans:beans xmlns:xsi      = "http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans    = "http://www.springframework.org/schema/beans"
             xmlns:jdbc     = "http://www.springframework.org/schema/jdbc"
             xmlns:int      = "http://www.springframework.org/schema/integration"
             xmlns:int-jdbc = "http://www.springframework.org/schema/integration/jdbc"
             xsi:schemaLocation="http://www.springframework.org/schema/beans            https://www.springframework.org/schema/beans/spring-beans.xsd
                                 http://www.springframework.org/schema/jdbc             https://www.springframework.org/schema/jdbc/spring-jdbc.xsd
                                 http://www.springframework.org/schema/integration      https://www.springframework.org/schema/integration/spring-integration.xsd
                                 http://www.springframework.org/schema/integration/jdbc https://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd">

    <int:gateway id="getAllCustomers-Gateway"
                 default-request-channel="getAllCustomers"
                 service-interface="demo.StringInputJsonOutputGatewayMethod" />

    <int:channel id="getAllCustomers" />

    <int-jdbc:outbound-gateway id="getAllCustomers-OutboundGateway"
                               request-channel="getAllCustomers"
                               query="select * from Customer"
                               data-source="dataSource"
                               max-rows="0" >
        <int-jdbc:request-handler-advice-chain>
            <beans:bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice" >
                <beans:property name="onSuccessExpressionString"     value="payload ?: {} " />
                <beans:property name="returnFailureExpressionResult" value="#{true}"        />
                <beans:property name="onFailureExpressionString"     value="{}"             />
            </beans:bean>
        </int-jdbc:request-handler-advice-chain>
    </int-jdbc:outbound-gateway>

    <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource" >
        <beans:property name="driverClass" value="org.h2.Driver" />
        <beans:property name="url"         value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" />
        <beans:property name="username"    value="sa" />
        <beans:property name="password"    value=""   />
    </beans:bean>

    <jdbc:initialize-database data-source="dataSource" >
        <jdbc:script location="classpath:/schema.sql" />
    </jdbc:initialize-database>
</beans:beans>

数据库初始化脚本(schema.sql)

CREATE TABLE Customer (
    ID         BIGINT      NOT NULL AUTO_INCREMENT,
    FIRST_NAME VARCHAR(30) NOT NULL,
    LAST_NAME  VARCHAR(30) NOT NULL,

    PRIMARY KEY (ID)
);

当前异常信息

org.springframework.integration.handler.ReplyRequiredException: No reply produced by handler 'getAllCustomers-OutboundGateway', and its 'requiresReply' property is set to true.

调试发现的问题

  • ExpressionEvaluatingRequestHandlerAdvice的successExpression会执行,但无法修改最终返回结果;
  • failureExpression未触发,因为JdbcOutboundGateway因空结果抛出的异常是在AdviceChain执行完成后才出现的;
  • 核心原因:JdbcOutboundGateway的handleRequestMessage()方法在接收到JDBC返回的空列表后,会直接返回null,关键代码片段如下:
if (this.poller != null) {
    ...
    list = this.poller.doPoll(sqlQueryParameterSource);
}
Object payload = list;
if (list.isEmpty()) {
    return null;
}

内容的提问来源于stack exchange,提问作者al.truisme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 16:30:15