【问题标题】:How to handle spaces in a jdbc outbound query parameter?如何处理 jdbc 出站查询参数中的空格?
【发布时间】:2018-10-23 19:33:33
【问题描述】:

我不知道这是否与集成相关,但在我的上下文中,我尝试通过包含空格的键来引用有效负载值:

<int-jdbc:outbound-channel-adapter 
    query="INSERT INTO table (serial, dealer_code) VALUES (:payload[SERIES], :payload[DEALER CODE])"
    data-source="dbDataSource">

</int-jdbc:outbound-channel-adapter>

虽然:payload[SERIES] 参数有效,但只要我不添加第二个:

  • :payload[DEALER CODE]
  • :payload['DEALER CODE']
  • :payload[&amp;quot;DEALER CODE&amp;quot;]

工作,而且最后两个用 a: 打断

org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied for the SQL parameter 'payload[': Invalid property 'payload[' of bean class [org.springframework.messaging.support.GenericMessage]: Bean property 'payload[' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

在其他地方,在另一个 enpoint 配置中,我确实使用引号:

<int-http:inbound-channel-adapter id="/api/requests"
    ...
    status-code-expression="T(org.springframework.web.context.request.RequestContextHolder).requestAttributes.request.method.equals('POST') ? 201 : 200">

</int-http:inbound-channel-adapter>

我已经解决了这个问题,添加了一个ExpressionEvaluatingSqlParameterSourceFactory(如Inbound Channel Adapter 中所述):

<int-jdbc:outbound-channel-adapter 
    query="INSERT INTO table (serial, dealer_code) VALUES (:serial, :dealerCode)"
    sql-parameter-source-factory="spelSource"
    data-source="dbDataSource">

</int-jdbc:outbound-channel-adapter>

<bean id="spelSource"
  class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
    <property name="parameterExpressions">
        <map>
            <entry key="serial" value="payload['SERIES']"/>
            <entry key="dealerCode" value="payload['DEALER CODE']"/>
        </map>
    </property>
</bean>

</int-jdbc:outbound-channel-adapter>

但是由于负载字段只是被读取,这似乎太多了。

我在这里使用了错误的语法吗?

【问题讨论】:

    标签: xml spring spring-integration


    【解决方案1】:

    尝试将ExpressionEvaluatingSqlParameterSourceFactory 用于&lt;int-jdbc:outbound-channel-adapter &gt;sql-parameter-source-factory 属性。是的,确实,该表达式必须是这样的::payload['DEALER CODE']

    【讨论】:

    • 我已经配置了源工厂但忘记显示它(更新了我的帖子):这是你的建议吗?因为:payload[SERIES] 在没有源工厂的情况下工作。
    • 不,你很好。默认情况下它使用 BeanPropertySqlParameterSourceFactory 的区别,它不是基于 SpEL 的。参数解析的问题最终出现在空格上。因此它将参数名称提取为payload['DEALER,这对您来说是错误的。因此,最好配置额外的 bean 以将您的目标键重新映射到可读的参数名称,就像您在最新配置中一样。
    猜你喜欢
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    相关资源
    最近更新 更多