Skip to content

Commit c766df9

Browse files
committed
Bael-9151, How to Map a Source Object to the Target List Using Mapstruct?
1 parent d6049e0 commit c766df9

File tree

11 files changed

+421
-2
lines changed

11 files changed

+421
-2
lines changed

mapstruct-2/pom.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@
5454
<version>${lombok.mapstruct.binding.version}</version>
5555
</path>
5656
</annotationProcessorPaths>
57+
<source>17</source>
58+
<target>17</target>
5759
</configuration>
5860
</plugin>
5961
</plugins>
6062
</build>
6163

6264
<properties>
63-
<org.mapstruct.version>1.6.0.Beta2</org.mapstruct.version>
65+
<org.mapstruct.version>1.6.3</org.mapstruct.version>
6466
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
6567
</properties>
6668

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.list;
2+
3+
import java.util.Arrays;
4+
5+
import org.mapstruct.Mapper;
6+
import org.mapstruct.Mapping;
7+
import org.mapstruct.factory.Mappers;
8+
9+
import com.baeldung.list.entity.Car;
10+
import com.baeldung.list.entity.CarDto;
11+
import com.baeldung.list.entity.ManufacturingPlantDto;
12+
13+
@Mapper(imports = { Arrays.class, ManufacturingPlantDto.class })
14+
public interface CarMapper {
15+
CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);
16+
17+
@Mapping(target = "numberOfSeats", source = "seats")
18+
@Mapping(
19+
target = "manufacturingPlantDtos",
20+
expression = """
21+
java(Arrays.asList(
22+
new ManufacturingPlantDto(car.getPlant1(), car.getPlant1Loc()),
23+
new ManufacturingPlantDto(car.getPlant2(), car.getPlant2Loc())
24+
))
25+
"""
26+
)
27+
CarDto carToCarDto(Car car);
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.list;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import com.baeldung.list.entity.Car;
10+
import com.baeldung.list.entity.CarDto;
11+
import com.baeldung.list.entity.ManufacturingPlantDto;
12+
13+
public abstract class CarMapperDecorator implements CustomCarMapper {
14+
private final Logger logger = LoggerFactory.getLogger(CarMapperDecorator.class);
15+
private CustomCarMapper delegate;
16+
17+
public CarMapperDecorator(CustomCarMapper delegate) {
18+
this.delegate = delegate;
19+
}
20+
21+
@Override
22+
public CarDto carToCarDto(Car car) {
23+
logger.info("calling Mapper decorator");
24+
CarDto carDto = delegate.carToCarDto(car);
25+
26+
carDto.setManufacturingPlantDtos(getManufacturingPlantDtos(car));
27+
28+
return carDto;
29+
}
30+
31+
private List<ManufacturingPlantDto> getManufacturingPlantDtos(Car car) {
32+
// some custom logic or transformation which may require calls to other services
33+
return Arrays.asList(
34+
new ManufacturingPlantDto(car.getPlant1(), car.getPlant1Loc()),
35+
new ManufacturingPlantDto(car.getPlant2(), car.getPlant2Loc())
36+
);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.list;
2+
3+
import org.mapstruct.DecoratedWith;
4+
import org.mapstruct.Mapper;
5+
import org.mapstruct.Mapping;
6+
import org.mapstruct.factory.Mappers;
7+
8+
import com.baeldung.list.entity.Car;
9+
import com.baeldung.list.entity.CarDto;
10+
11+
@Mapper
12+
@DecoratedWith(CarMapperDecorator.class)
13+
public interface CustomCarMapper {
14+
CustomCarMapper INSTANCE = Mappers.getMapper(CustomCarMapper.class);
15+
16+
@Mapping(source = "seats", target = "numberOfSeats")
17+
CarDto carToCarDto(Car car);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.list;
2+
3+
import java.util.List;
4+
5+
import org.mapstruct.Mapper;
6+
import org.mapstruct.Mapping;
7+
import org.mapstruct.Named;
8+
import org.mapstruct.factory.Mappers;
9+
10+
import com.baeldung.list.entity.Car;
11+
import com.baeldung.list.entity.CarDto;
12+
import com.baeldung.list.entity.ManufacturingPlantDto;
13+
14+
@Mapper
15+
public interface QualifiedByNameCarMapper {
16+
QualifiedByNameCarMapper INSTANCE = Mappers.getMapper(QualifiedByNameCarMapper.class);
17+
18+
@Mapping(source = "seats", target = "numberOfSeats")
19+
@Mapping(target = "manufacturingPlantDtos", source = "car", qualifiedByName = "mapPlants")
20+
CarDto carToCarDto(Car car);
21+
22+
@Named("mapPlants")
23+
default List<ManufacturingPlantDto> mapPlants(Car car) {
24+
return List.of(
25+
new ManufacturingPlantDto(car.getPlant1(), car.getPlant1Loc()),
26+
new ManufacturingPlantDto(car.getPlant2(), car.getPlant2Loc())
27+
);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung.list.entity;
2+
3+
public class Car {
4+
private String make;
5+
private String model;
6+
private int year;
7+
private int seats;
8+
9+
private String plant1;
10+
private String plant1Loc;
11+
private String plant2;
12+
private String plant2Loc;
13+
14+
public Car(String make, String model, int year, int seats) {
15+
this.make = make;
16+
this.model = model;
17+
this.year = year;
18+
this.seats = seats;
19+
}
20+
21+
public String getPlant2() {
22+
return plant2;
23+
}
24+
25+
public void setPlant2(String plant2) {
26+
this.plant2 = plant2;
27+
}
28+
29+
public String getPlant2Loc() {
30+
return plant2Loc;
31+
}
32+
33+
public void setPlant2Loc(String plant2Loc) {
34+
this.plant2Loc = plant2Loc;
35+
}
36+
37+
public String getPlant1Loc() {
38+
return plant1Loc;
39+
}
40+
41+
public void setPlant1Loc(String plant1Loc) {
42+
this.plant1Loc = plant1Loc;
43+
}
44+
45+
public String getPlant1() {
46+
return plant1;
47+
}
48+
49+
public void setPlant1(String plant1) {
50+
this.plant1 = plant1;
51+
}
52+
53+
public int getSeats() {
54+
return seats;
55+
}
56+
57+
public void setSeats(int seats) {
58+
this.seats = seats;
59+
}
60+
61+
public String getMake() {
62+
return make;
63+
}
64+
65+
public String getModel() {
66+
return model;
67+
}
68+
69+
public int getYear() {
70+
return year;
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.list.entity;
2+
3+
import java.util.List;
4+
5+
public class CarDto {
6+
private String make;
7+
private String model;
8+
private int year;
9+
private int numberOfSeats;
10+
11+
private List<ManufacturingPlantDto> manufacturingPlantDtos;
12+
13+
public CarDto(String make, String model, int year, int numberOfSeats) {
14+
this.make = make;
15+
this.model = model;
16+
this.year = year;
17+
this.numberOfSeats = numberOfSeats;
18+
}
19+
20+
public void setMake(String make) {
21+
this.make = make;
22+
}
23+
24+
public void setModel(String model) {
25+
this.model = model;
26+
}
27+
28+
public void setYear(int year) {
29+
this.year = year;
30+
}
31+
32+
public List<ManufacturingPlantDto> getManufacturingPlantDtos() {
33+
return manufacturingPlantDtos;
34+
}
35+
36+
public void setManufacturingPlantDtos(List<ManufacturingPlantDto> manufacturingPlantDtos) {
37+
this.manufacturingPlantDtos = manufacturingPlantDtos;
38+
}
39+
40+
41+
public int getNumberOfSeats() {
42+
return numberOfSeats;
43+
}
44+
45+
public void setNumberOfSeats(int numberOfSeats) {
46+
this.numberOfSeats = numberOfSeats;
47+
}
48+
49+
public String getMake() {
50+
return make;
51+
}
52+
53+
public String getModel() {
54+
return model;
55+
}
56+
57+
public int getYear() {
58+
return year;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.list.entity;
2+
3+
public class ManufacturingPlantDto {
4+
private String plantName;
5+
private String location;
6+
7+
public ManufacturingPlantDto(String plantName, String location) {
8+
this.plantName = plantName;
9+
this.location = location;
10+
}
11+
12+
public String getPlantName() {
13+
return plantName;
14+
}
15+
16+
public void setPlantName(String plantName) {
17+
this.plantName = plantName;
18+
}
19+
20+
public String getLocation() {
21+
return location;
22+
}
23+
24+
public void setLocation(String location) {
25+
this.location = location;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
@startuml
2+
'https://plantuml.com/class-diagram
3+
hide empty attributes
4+
skinparam Handwritten false
5+
skinparam ClassBorderColor black
6+
skinparam BackgroundColor #fffce8/#f8f9fa
7+
skinparam class {
8+
ArrowColor SeaGreen
9+
BackgroundColor #fffce8
10+
}
11+
12+
class Car {
13+
-make: String
14+
-model: String
15+
-year: int
16+
-seats: int
17+
-plant1: String
18+
-plant2: String
19+
-plant1Loc: String
20+
-plant2Loc: String
21+
__
22+
+Car(make: String, model: String, year: int, seats: int)
23+
__
24+
<i>Standard getter and setter methods
25+
}
26+
27+
class CarDto {
28+
-make: String
29+
-model: String
30+
-year: int
31+
-numberOfseats: int
32+
-manufacturingPlantDtos:List<ManufacturingPlantDto>
33+
__
34+
+CarDto(make: String, model: String, year: int, numberOfSeats: int)
35+
__
36+
<i>Standard getter and setter methods
37+
}
38+
39+
40+
41+
class ManufacturingPlantDto {
42+
-plantName: String
43+
-location: String
44+
__
45+
+ManufacturingPlantDto(plantName: String, location: String)
46+
__
47+
<i>Standard getter and setter methods
48+
}
49+
50+
note left of Car
51+
Source object
52+
end note
53+
54+
note left of CarDto
55+
Target object derived from Car
56+
end note
57+
58+
note left of ManufacturingPlantDto::plantName
59+
possible values mapped to
60+
Car#plant1 and Car#plant2
61+
end note
62+
note left of ManufacturingPlantDto::location
63+
possible values mapped to
64+
Car#plant1Loc and Car#plant2Loc
65+
end note
66+
67+
CarDto -down-> ManufacturingPlantDto: 1\n*
68+
Car .down. CarDto: 1\n1
69+
@enduml

0 commit comments

Comments
 (0)