java Apache CamelMap结构集成

a0x5cqrl  于 2023-02-11  发布在  Java
关注(0)|答案(1)|浏览(101)

在我的Springboot Camel微服务中,我将camel mapstruct与以下依赖项集成在一起:

<camel.version>3.20.1</camel.version>
<mapstruct.version>1.5.3.Final</mapstruct.version>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-spring-boot-starter</artifactId>
    <version>${camel.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-mapstruct-starter</artifactId>
    <version>${camel.version}</version>
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>${mapstruct.version}</version>
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>${mapstruct.version}</version>
    <scope>provided</scope>
</dependency>

我也有一个这样的Map器:

@Mapper(componentModel = "spring")
public interface CamelTestMapper {

    @BeanMapping(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
    CamelTestDto mapCamelTestEntity(CamelTestEntity camelTestEntity);

}

还有这样的路线

from("direct:get-by-id")
                .routeId("db-get-by-id-route")
                .transacted()
                .process(camelTestSampleDatabaseProcessor)
                .log(LoggingLevel.INFO, "Retrieved entity from db with name: ${body.name}")
                .to("mapstruct:org.example.model.dto.CamelTestDto")
                .log(LoggingLevel.INFO, "Mapped new Dto: ${body}");

我不明白为什么会这样,当我使用

.convertBodyTo(CamelTestDto.class)

它将抛出以下异常:

org.apache.camel.InvalidPayloadException: No body available of type: org.example.model.dto.CamelTestDto but has type: org.example.model.entity.CamelTestEntity on: Message[4939598FD4DFE7C-0000000000000103]. 
    Caused by: No type converter available to convert from type: org.example.model.entity.CamelTestEntity to the required type: org.example.model.dto.CamelTestDto. Exchange[4939598FD4DFE7C-0000000000000103]. 
    Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.example.model.entity.CamelTestEntity to the required type: org.example.model.dto.CamelTestDto]

显然,我已经以这种方式设置了application .yml:

camel:
  component:
    mapstruct:
      mapper-package-name: org.example.mapper

有人知道如何解决这个问题吗?你能给予我一些例子,如何使用这个集成?也与转换与列表请。

dgtucam1

dgtucam11#

我还没有尝试过这种方法,但我猜Camel想要一个被注解为类型转换器的bean来用于.convertBodyTo指令。
由于实际的转换器是一个生成的类,我不确定您将如何做到这一点......也许有一种方法可以在Camel读取的XML文件中定义它。

相关问题