|
| 1 | +# MapStruct with Kotlin |
| 2 | + |
| 3 | +Example usage of Mapstruct and Kotlin with JDK-8 Bytecode. This is achieved by using the [Kotlin Annotation Processing Tool (KAPT)](https://kotlinlang.org/docs/reference/kapt.html). |
| 4 | + |
| 5 | +## Code |
| 6 | + |
| 7 | +In this example we want to map between a Person (Model) and a PersonDto (DTO). |
| 8 | + |
| 9 | +```kotlin |
| 10 | +data class Person(var firstName: String?, var lastName: String?, var phoneNumber: String?, var birthdate: LocalDate?) { |
| 11 | + // Necessary for MapStruct |
| 12 | + constructor() : this(null, null, null, null) |
| 13 | +} |
| 14 | +``` |
| 15 | + |
| 16 | +```kotlin |
| 17 | +data class PersonDto(var firstName: String?, var lastName: String?, var phone: String?, var birthdate: LocalDate?) { |
| 18 | + // Necessary for MapStruct |
| 19 | + constructor() : this(null, null, null, null) |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +The MapStruct converter: |
| 24 | + |
| 25 | +```kotlin |
| 26 | +@Mapper |
| 27 | +interface PersonConverter { |
| 28 | + |
| 29 | + @Mapping(source = "phoneNumber", target = "phone") |
| 30 | + fun convertToDto(person: Person) : PersonDto |
| 31 | + |
| 32 | + @InheritInverseConfiguration |
| 33 | + fun convertToModel(personDto: PersonDto) : Person |
| 34 | + |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Usage: |
| 39 | + |
| 40 | +```kotlin |
| 41 | +val converter = Mappers.getMapper(PersonConverter::class.java) // or PersonConverterImpl() |
| 42 | + |
| 43 | +val person = Person("Samuel", "Jackson", "0123 334466", LocalDate.of(1948, 12, 21)) |
| 44 | + |
| 45 | +val personDto = converter.convertToDto(person) |
| 46 | +println(personDto) |
| 47 | + |
| 48 | +val personModel = converter.convertToModel(personDto) |
| 49 | +println(personModel) |
| 50 | +``` |
| 51 | + |
| 52 | +## Further documentation |
| 53 | + |
| 54 | +Using kapt: https://kotlinlang.org/docs/reference/kapt.html |
| 55 | + |
| 56 | +MapStruct: http://mapstruct.org/ |
| 57 | + |
| 58 | +Kotlin: https://kotlinlang.org/ |
| 59 | + |
| 60 | +## Maven Setup |
| 61 | + |
| 62 | +Add Mapstruct-JDK8 to your dependencies |
| 63 | + |
| 64 | +```xml |
| 65 | +<dependency> |
| 66 | + <groupId>org.mapstruct</groupId> |
| 67 | + <artifactId>mapstruct-jdk8</artifactId> |
| 68 | + <version>${mapstruct.version}</version> |
| 69 | +</dependency> |
| 70 | +``` |
| 71 | + |
| 72 | +Add an execution to the kotlin-maven-plugin with the kapt goal and mapstruct as annotation processor: |
| 73 | + |
| 74 | +```xml |
| 75 | + |
| 76 | +<plugin> |
| 77 | + <groupId>org.jetbrains.kotlin</groupId> |
| 78 | + <artifactId>kotlin-maven-plugin</artifactId> |
| 79 | + <version>${kotlin.version}</version> |
| 80 | + <executions> |
| 81 | + <execution> |
| 82 | + <id>kapt</id> |
| 83 | + <goals> |
| 84 | + <goal>kapt</goal> |
| 85 | + </goals> |
| 86 | + <configuration> |
| 87 | + <sourceDirs> |
| 88 | + <sourceDir>src/main/kotlin</sourceDir> |
| 89 | + <sourceDir>src/main/java</sourceDir> |
| 90 | + </sourceDirs> |
| 91 | + <annotationProcessorPaths> |
| 92 | + <annotationProcessorPath> |
| 93 | + <groupId>org.mapstruct</groupId> |
| 94 | + <artifactId>mapstruct-processor</artifactId> |
| 95 | + <version>${mapstruct.version}</version> |
| 96 | + </annotationProcessorPath> |
| 97 | + </annotationProcessorPaths> |
| 98 | + </configuration> |
| 99 | + </execution> |
| 100 | + ... |
| 101 | + </executions> |
| 102 | + .... |
| 103 | +</plugin> |
| 104 | +``` |
0 commit comments