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

Apache Camel中oracle.sql.TIMESTAMP转java.util.Date映射错误求助

问题描述

使用Apache Camel框架从Oracle数据库获取数据时,数据库中timestamp类型的列无法映射到Java变量,抛出错误:

Caused by: java.lang.IllegalArgumentException:
Could not find a suitable setter for property lastupdated as there isn't a setter method with same type oracle.sql.TIMESTAMP nor type conversion possible:
No type converter available to convert from type oracle.sql.TIMESTAMP to the required type java.util.Date with value "2022-11-16 19:04:27.067"

已尝试过以下Java数据类型,但均报错:

  • java.util.Date
  • oracle.sql.TIMESTAMP
  • java.sql.Timestamp
解决方案

1. 补充Camel Oracle类型转换依赖

如果使用Maven,将以下依赖添加到pom.xml,确保Camel拥有Oracle类型转换的基础支持:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jdbc</artifactId>
    <version>${camel.version}</version> <!-- 替换为你项目的Camel版本 -->
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core-converters</artifactId>
    <version>${camel.version}</version>
</dependency>

2. 自定义类型转换器

实现专属的TypeConverter处理oracle.sql.TIMESTAMP到目标Java类型的转换:

import oracle.sql.TIMESTAMP;
import org.apache.camel.Converter;
import java.util.Date;
import java.sql.Timestamp;

@Converter
public class OracleTimestampConverter {

    @Converter
    public static Date toUtilDate(TIMESTAMP oracleTs) {
        return oracleTs == null ? null : new Date(oracleTs.timestampValue().getTime());
    }

    @Converter
    public static Timestamp toSqlTimestamp(TIMESTAMP oracleTs) {
        return oracleTs == null ? null : oracleTs.timestampValue();
    }
}

注册转换器

  • 纯Java环境:
CamelContext context = new DefaultCamelContext();
context.getTypeConverterRegistry().addTypeConverter(Date.class, TIMESTAMP.class, new OracleTimestampConverter());
context.getTypeConverterRegistry().addTypeConverter(Timestamp.class, TIMESTAMP.class, new OracleTimestampConverter());
  • Spring Boot环境:给转换器类加上@Component注解,Camel会自动扫描并注册:
import org.springframework.stereotype.Component;

@Component
@Converter
public class OracleTimestampConverter {
    // 转换方法同上
}

3. SQL查询中显式转换字段

在查询语句中将Oracle的timestamp转换为标准SQL类型,避免返回oracle.sql.TIMESTAMP:

SELECT CAST(lastupdated AS TIMESTAMP) AS lastupdated FROM your_target_table

这样Camel会收到标准的java.sql.Timestamp,更容易映射到java.util.Date或java.sql.Timestamp变量。

4. 检查实体类Setter方法

确保实体类中lastupdated属性的Setter方法参数类型与目标类型完全匹配:

  • 若目标类型为java.sql.Timestamp:
public void setLastupdated(Timestamp lastupdated) {
    this.lastupdated = lastupdated;
}
  • 若目标类型为java.util.Date:
public void setLastupdated(Date lastupdated) {
    this.lastupdated = lastupdated;
}

内容的提问来源于stack exchange,提问作者Dheeraj Giri

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 13:25:15