Skip to content

Commit 3698884

Browse files
BAEL-8521: Mapping to String in Mapstruct (#19086)
* BAEL-8521: Mapping to String in Mapstruct * BAEL-9429: Clean up pom.xml for MapStruct string mapping examples * BAEL-8521: Mapping to String in Mapstruct
1 parent 7dc0d1a commit 3698884

14 files changed

Lines changed: 331 additions & 2 deletions

File tree

core-java-modules/core-java-string-conversions-4/pom.xml

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
56
<modelVersion>4.0.0</modelVersion>
67
<artifactId>core-java-string-conversions-4</artifactId>
78
<packaging>jar</packaging>
@@ -14,32 +15,86 @@
1415
</parent>
1516

1617
<dependencies>
18+
<!-- ICU4J for internationalization utilities -->
1719
<dependency>
1820
<groupId>com.ibm.icu</groupId>
1921
<artifactId>icu4j</artifactId>
2022
<version>${icu4j.version}</version>
2123
</dependency>
24+
25+
<!-- Apache Commons Text for text manipulation -->
2226
<dependency>
2327
<groupId>org.apache.commons</groupId>
2428
<artifactId>commons-text</artifactId>
2529
<version>${commons-text.version}</version>
2630
</dependency>
31+
32+
<!-- MapStruct for compile-time mapping -->
33+
<dependency>
34+
<groupId>org.mapstruct</groupId>
35+
<artifactId>mapstruct</artifactId>
36+
<version>1.6.0</version>
37+
</dependency>
38+
39+
<!-- JUnit 5 for testing -->
40+
<dependency>
41+
<groupId>org.junit.jupiter</groupId>
42+
<artifactId>junit-jupiter</artifactId>
43+
<version>5.10.2</version>
44+
<scope>test</scope>
45+
</dependency>
2746
</dependencies>
2847

2948
<build>
3049
<finalName>core-java-string-conversions-4</finalName>
50+
3151
<resources>
3252
<resource>
3353
<directory>src/main/resources</directory>
3454
<filtering>true</filtering>
3555
</resource>
3656
</resources>
57+
58+
<plugins>
59+
<!-- Compiler plugin for Java 17 and MapStruct annotation processing -->
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-compiler-plugin</artifactId>
63+
<version>3.11.0</version>
64+
<configuration>
65+
<source>17</source>
66+
<target>17</target>
67+
<annotationProcessorPaths>
68+
<path>
69+
<groupId>org.mapstruct</groupId>
70+
<artifactId>mapstruct-processor</artifactId>
71+
<version>1.6.0</version>
72+
</path>
73+
</annotationProcessorPaths>
74+
<compilerArgs>
75+
<arg>-parameters</arg>
76+
</compilerArgs>
77+
</configuration>
78+
</plugin>
79+
80+
<!-- Surefire plugin to run unit tests -->
81+
<plugin>
82+
<groupId>org.apache.maven.plugins</groupId>
83+
<artifactId>maven-surefire-plugin</artifactId>
84+
<version>3.1.2</version>
85+
<configuration>
86+
<includes>
87+
<include>**/*UnitTest.java</include>
88+
</includes>
89+
</configuration>
90+
</plugin>
91+
</plugins>
3792
</build>
3893

3994
<properties>
4095
<icu4j.version>61.1</icu4j.version>
41-
<argLine>-Djava.locale.providers=COMPAT</argLine>
4296
<commons-text.version>1.9</commons-text.version>
97+
<argLine>-Djava.locale.providers=COMPAT</argLine>
98+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4399
</properties>
44-
45100
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.mapstructstringmapping;
2+
3+
import java.time.LocalDate;
4+
5+
public class Event {
6+
7+
private String name;
8+
private LocalDate eventDate;
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
18+
public LocalDate getEventDate() {
19+
return eventDate;
20+
}
21+
22+
public void setEventDate(LocalDate eventDate) {
23+
this.eventDate = eventDate;
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.mapstructstringmapping;
2+
3+
public class EventDTO {
4+
5+
private String name;
6+
private String eventDate;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
this.name = name;
14+
}
15+
16+
public String getEventDate() {
17+
return eventDate;
18+
}
19+
20+
public void setEventDate(String eventDate) {
21+
this.eventDate = eventDate;
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.mapstructstringmapping;
2+
3+
import org.mapstruct.Mapper;
4+
import org.mapstruct.Mapping;
5+
import org.mapstruct.factory.Mappers;
6+
7+
@Mapper
8+
public interface EventMapper {
9+
10+
EventMapper INSTANCE = Mappers.getMapper(EventMapper.class);
11+
12+
@Mapping(source = "eventDate", target = "eventDate", dateFormat = "yyyy-MM-dd")
13+
EventDTO toEventDTO(Event event);
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.mapstructstringmapping;
2+
3+
public class Person {
4+
5+
private String name;
6+
private int age;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
this.name = name;
14+
}
15+
16+
public int getAge() {
17+
return age;
18+
}
19+
20+
public void setAge(int age) {
21+
this.age = age;
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.mapstructstringmapping;
2+
3+
public class PersonDTO {
4+
5+
private String name;
6+
private String age;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
this.name = name;
14+
}
15+
16+
public String getAge() {
17+
return age;
18+
}
19+
20+
public void setAge(String age) {
21+
this.age = age;
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.mapstructstringmapping;
2+
3+
import org.mapstruct.Mapper;
4+
import org.mapstruct.Mapping;
5+
import org.mapstruct.NullValueMappingStrategy;
6+
import org.mapstruct.Named;
7+
import org.mapstruct.factory.Mappers;
8+
9+
@Mapper(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
10+
public interface PersonMapper {
11+
12+
PersonMapper INSTANCE = Mappers.getMapper(PersonMapper.class);
13+
14+
@Mapping(target = "name", qualifiedByName = "mapName")
15+
@Mapping(target = "age", expression = "java(String.valueOf(person.getAge()))")
16+
PersonDTO toDTO(Person person);
17+
18+
@Named("mapName")
19+
default String mapName(String name) {
20+
return name == null ? "Unknown" : name;
21+
}
22+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.mapstructstringmapping;
2+
3+
public enum Status {
4+
ACTIVE,
5+
INACTIVE,
6+
PENDING
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.mapstructstringmapping;
2+
3+
public class User {
4+
5+
private String username;
6+
private Status status;
7+
8+
public String getUsername() {
9+
return username;
10+
}
11+
12+
public void setUsername(String username) {
13+
this.username = username;
14+
}
15+
16+
public Status getStatus() {
17+
return status;
18+
}
19+
20+
public void setStatus(Status status) {
21+
this.status = status;
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.mapstructstringmapping;
2+
3+
public class UserDTO {
4+
5+
private String username;
6+
private String status;
7+
8+
public String getUsername() {
9+
return username;
10+
}
11+
12+
public void setUsername(String username) {
13+
this.username = username;
14+
}
15+
16+
public String getStatus() {
17+
return status;
18+
}
19+
20+
public void setStatus(String status) {
21+
this.status = status;
22+
}
23+
}

0 commit comments

Comments
 (0)