4

I have following route which get data from postgresql but date object is coming null. it's unable to map that value

My route if following

<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>

My Pojo class looks like

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 schema is

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

I am unable to map jodadatetime and every time it's coming null. Please help me out how could we map this in camel jdbc

user1047873
  • 230
  • 3
  • 8
  • 28

1 Answers1

2

You need to add a type converter for org.joda.time.DateTime so Camel can convert the row value to an instance of joda time. Camel does not come with these converters out of the box.

An alternative is to use java.lang.Object as the parameter type in your POJO class, and do the type conversion in the setter.

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • How could i add type converter would you mind me giving some more details on that and if you have some gthub code that would be really helpful – user1047873 Dec 26 '17 at 06:29
  • See http://camel.apache.org/type-converter.html - its also covered in chapter 3 in the Camel in Action 2nd edition book (and also in 1st edition) – Claus Ibsen Dec 26 '17 at 08:31
  • I have added following Type converter but no help as it's not getting picked up. I am using spring based camel and added bean for this import org.apache.camel.Exchange; import org.apache.camel.TypeConversionException; import org.apache.camel.support.TypeConverterSupport; import 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) DateTime.now(); } } – user1047873 Dec 26 '17 at 12:09
  • I used following link as well https://github.com/jankronquist/AirportWeather but no help. I am not sure whether type converter works when we get data from Database – user1047873 Dec 26 '17 at 12:35