【问题标题】:How to automatically convert from org.apache.camel.converter.stream.InputStreamCache to Pojo using Jackson in a Spring Boot Camel Kotlin application如何在 Spring Boot Camel Kotlin 应用程序中使用 Jackson 自动从 org.apache.camel.converter.stream.InputStreamCache 转换为 Pojo
【发布时间】:2023-02-16 03:51:18
【问题描述】:

在 Spring Boot 2.7 中。用 Kotlin 编写的 Camel 3.20.x 项目 我有一个接收 JSON 负载的 REST 端点。我添加了 Camel Jackson 依赖项来处理 JSON<->POJO 转换:

        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-jackson-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
data class Payment(val iban: String, val amount: Float)
    rest("/payments")
            .post("/")
            .to("direct:processPayment")

    from("direct:processPayment")
            .log("Body \${body}")
            .log("Body \${body.getClass()}")

这些是路线的日志:

Body {"payment":{"iban":"ABCD","amount":150.0}}
Body class org.apache.camel.converter.stream.InputStreamCache

如您所见,正文正确显示为字符串,但是,类型是 InputStreamCache 而不是我的付款 DTO。

我更新了将正文解组到支付 DTO 的路由:

    from("direct:processPayment")
        .unmarshal().json(JsonLibrary.Jackson, Payment::class.java)
        .log("Body \${body}")
        .log("Body \${body.getClass()}")

这失败了:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `xxx.Payment` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

为什么转换不起作用?

【问题讨论】:

  • 你能展示你的付款课程吗?是否有默认构造函数(没有争论) ?
  • 不然试试介绍个.convertBodyTo(String.class)解组
  • @TacheDeChoco 不,它没有默认构造函数,我使用的是 Kotlin 数据类
  • 您的 pojo 需要遵守 Java Bean 约定(例如使用默认构造函数),否则解组无法工作
  • 还要检查您的 pojo 是否带有 @XmlRootElement 注释

标签: kotlin apache-camel spring-camel


【解决方案1】:

遇到的错误与 Camel 和 Spring 无关,但与 JAXB 直接相关。

如错误消息所示,原因是 Payment pojo 不满足具有默认值的 JAXB 要求(无参数) 构造函数。

【讨论】:

    猜你喜欢
    • 2019-07-25
    • 1970-01-01
    • 2022-12-20
    • 2016-01-28
    • 2014-01-31
    • 2020-07-29
    • 2020-09-07
    • 2021-01-24
    • 1970-01-01
    相关资源
    最近更新 更多