Search code examples
javaspring-bootapache-camelmapstruct

Apache Camel Mapstruct integration


In my Springboot Camel microservice I integrate camel mapstruct with this dependency:

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

I also have a mapper like this:

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

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

}

and a route like this:

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}");

I can't understand why in this way it works, and when I use

.convertBodyTo(CamelTestDto.class)

it throw this exception:

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]

Obviously I already set the application .yml in this way:

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

Does anyone know how to solve it? Can you give to me some examples of how to use this integrations? Also with conversion with List please.


Solution

  • There was a recent bug in camel-mapstruct-starter for SB that it did not auto-configure itself correctly, when you did not use mapstruct as a component in the routes.

    This will be fixed in upcoming Camel releases: https://issues.apache.org/jira/browse/CAMEL-19181