【问题标题】:Unable to map postgresql timestamp with timezone in camel jdbc无法在骆驼 jdbc 中将 postgresql 时间戳与时区映射
【发布时间】:2018-06-03 23:53:35
【问题描述】:

我有以下从 postgresql 获取数据的路线,但日期对象为空。它无法映射该值

如果跟随我的路线

<route id="instrumentqueryshortsell">
        <from uri="direct:restinstumentshortsell"/>
        <bean ref="inConverter" method="convert"/>
        <setBody>
            <simple>select instrumentId as instrument_Id ,amount,easytoborrow as easy_To_Borrow,hardtoborrow as hard_To_Borrow ,datetime as date from instrument_short_sell where instrumentId in (${body}) </simple>
        </setBody>
        <log message="Running following query ${body} " loggingLevel="DEBUG"/>
        <to uri="jdbc:dataSource?useHeadersAsParameters=true&amp;outputClass=<packagename?.InstrumentShortSell" />

    </route>

我的 Pojo 类看起来像

import java.time.LocalDateTime;

import org.joda.time.DateTime;


public class InstrumentShortSell {

    private String instrumentId;
    private long amount;
    private boolean easyToBorrow;
    private boolean hardToBorrow;
    private DateTime date;

    public DateTime getDate() {
        return date;
    }
    public void setDate(DateTime date) {
        this.date = date;
    }
    public String getInstrumentId() {
        return instrumentId;
    }
    public void setInstrumentId(String instrumentId) {
        this.instrumentId = instrumentId;
    }
    public long getAmount() {
        return amount;
    }
    public void setAmount(long amount) {
        this.amount = amount;
    }
    public boolean isEasyToBorrow() {
        return easyToBorrow;
    }
    public void setEasyToBorrow(boolean easyToBorrow) {
        this.easyToBorrow = easyToBorrow;
    }
    public boolean isHardToBorrow() {
        return hardToBorrow;
    }
    public void setHardToBorrow(boolean hardToBorrow) {
        this.hardToBorrow = hardToBorrow;
    }


}

SQL 架构是

CREATE TABLE instrument_short_sell (
    instrumentId serial ,
    amount INTEGER,
    easytoborrow boolean,
    hardtoborrow boolean,
    datetime timestamp with time zone
) ;

我无法映射 jodadatetime 并且每次它都为空。请帮助我如何在骆驼 jdbc 中映射它

【问题讨论】:

  • 您是否尝试使用 java.lang.Date 而不是 org.joda.time.DateTime
  • java.lang.Date 无论如何都没有时区 java.lang.Date 工作得很好,但我在 Joda datetime 中需要这个

标签: java apache-camel camel-sql


【解决方案1】:

您需要为 org.joda.time.DateTime 添加一个类型转换器,以便 Camel 可以将行值转换为 joda time 的实例。 Camel 没有自带这些转换器。

另一种方法是使用java.lang.Object 作为 POJO 类中的参数类型,并在 setter 中进行类型转换。

【讨论】:

  • 我怎样才能添加类型转换器,你介意我提供更多细节吗?如果你有一些真正有用的 gthub 代码
  • camel.apache.org/type-converter.html - 它也包含在 Camel in Action 第 2 版书(以及第 1 版)的第 3 章中
  • 我添加了以下类型转换器,但没有帮助,因为它没有被拾取。我正在使用基于弹簧的骆驼并为此导入 org.apache.camel.Exchange 添加了 bean;导入 org.apache.camel.TypeConversionException;导入 org.apache.camel.support.TypeConverterSupport;导入 org.joda.time.DateTime; public class DateTimeTypeConverter extends TypeConverterSupport { @Override public T convertTo(Class type, Exchange exchange, Object value) throws TypeConversionException { if(type== DateTime.class && value != null){ } return (T)日期时间.now(); } }
  • 我也使用了以下链接github.com/jankronquist/AirportWeather,但没有帮助。我不确定当我们从数据库中获取数据时类型转换器是否有效
猜你喜欢
  • 2013-03-27
  • 2017-12-27
  • 2010-11-02
  • 2015-04-10
  • 2012-11-01
  • 2019-11-17
  • 1970-01-01
  • 2018-12-30
  • 1970-01-01
相关资源
最近更新 更多