Skip to content

Commit 683b4bf

Browse files
committed
BAEL-9511 - MapStruct Null Handling
1 parent c2cdc9d commit 683b4bf

15 files changed

+391
-2
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.dto;
2+
3+
public class AncestorDto {
4+
5+
private String firstName;
6+
7+
private Double weight;
8+
9+
private String vatNumber;
10+
11+
public AncestorDto() {
12+
}
13+
14+
public AncestorDto(String firstName, Double weight, String vatNumber) {
15+
this.firstName = firstName;
16+
this.weight = weight;
17+
this.vatNumber = vatNumber;
18+
}
19+
20+
public String getFirstName() {
21+
return firstName;
22+
}
23+
24+
public void setFirstName(String firstName) {
25+
this.firstName = firstName;
26+
}
27+
28+
public Double getWeight() {
29+
return weight;
30+
}
31+
32+
public void setWeight(Double weight) {
33+
this.weight = weight;
34+
}
35+
36+
public String getVatNumber() {
37+
return vatNumber;
38+
}
39+
40+
public void setVatNumber(String vatNumber) {
41+
this.vatNumber = vatNumber;
42+
}
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.dto;
2+
3+
import java.util.List;
4+
5+
public class OrderDto {
6+
7+
private String transactionId;
8+
9+
private List<String> orderItemIds;
10+
11+
private PaymentDto paymentDto;
12+
13+
public OrderDto(String transactionId, List<String> orderItemIds, PaymentDto paymentDto) {
14+
this.transactionId = transactionId;
15+
this.orderItemIds = orderItemIds;
16+
this.paymentDto = paymentDto;
17+
}
18+
19+
public OrderDto() {
20+
}
21+
22+
public String getTransactionId() {
23+
return transactionId;
24+
}
25+
26+
public void setTransactionId(String transactionId) {
27+
this.transactionId = transactionId;
28+
}
29+
30+
public List<String> getOrderItemIds() {
31+
return orderItemIds;
32+
}
33+
34+
public void setOrderItemIds(List<String> orderItemIds) {
35+
this.orderItemIds = orderItemIds;
36+
}
37+
38+
public PaymentDto getPaymentDto() {
39+
return paymentDto;
40+
}
41+
42+
public void setPaymentDto(PaymentDto paymentDto) {
43+
this.paymentDto = paymentDto;
44+
}
45+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.dto;
2+
3+
public class PaymentDto {
4+
5+
private String type;
6+
7+
public PaymentDto() {
8+
}
9+
10+
public PaymentDto(String type) {
11+
this.type = type;
12+
}
13+
14+
public String getType() {
15+
return type;
16+
}
17+
18+
public void setType(String type) {
19+
this.type = type;
20+
}
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.entity;
2+
3+
public class GrandDad {
4+
5+
private Identity identity;
6+
7+
private String weight;
8+
9+
private String vatNumber;
10+
11+
public GrandDad() {
12+
}
13+
14+
public GrandDad(Identity identity, String weight, String vatNumber) {
15+
this.identity = identity;
16+
this.weight = weight;
17+
this.vatNumber = vatNumber;
18+
}
19+
20+
public Identity getIdentity() {
21+
return identity;
22+
}
23+
24+
public void setIdentity(Identity identity) {
25+
this.identity = identity;
26+
}
27+
28+
public String getWeight() {
29+
return weight;
30+
}
31+
32+
public void setWeight(String weight) {
33+
this.weight = weight;
34+
}
35+
36+
public String getVatNumber() {
37+
return vatNumber;
38+
}
39+
40+
public void setVatNumber(String vatNumber) {
41+
this.vatNumber = vatNumber;
42+
}
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.entity;
2+
3+
public class Identity {
4+
5+
private String name;
6+
private String surname;
7+
8+
public Identity() {
9+
}
10+
11+
public Identity(String name, String surname) {
12+
this.name = name;
13+
this.surname = surname;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getSurname() {
25+
return surname;
26+
}
27+
28+
public void setSurname(String surname) {
29+
this.surname = surname;
30+
}
31+
32+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.entity;
2+
3+
import java.util.List;
4+
5+
public class Order {
6+
7+
private String transactionId;
8+
9+
private List<String> orderItemIds;
10+
11+
private Payment payment;
12+
13+
public Order(String transactionId, List<String> orderItemIds, Payment payment) {
14+
this.transactionId = transactionId;
15+
this.orderItemIds = orderItemIds;
16+
this.payment = payment;
17+
}
18+
19+
public Order() {
20+
}
21+
22+
public String getTransactionId() {
23+
return transactionId;
24+
}
25+
26+
public void setTransactionId(String transactionId) {
27+
this.transactionId = transactionId;
28+
}
29+
30+
public List<String> getOrderItemIds() {
31+
return orderItemIds;
32+
}
33+
34+
public void setOrderItemIds(List<String> orderItemIds) {
35+
this.orderItemIds = orderItemIds;
36+
}
37+
38+
public Payment getPayment() {
39+
return payment;
40+
}
41+
42+
public void setPayment(Payment payment) {
43+
this.payment = payment;
44+
}
45+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.entity;
2+
3+
public class Payment {
4+
5+
private String type;
6+
7+
public Payment() {
8+
}
9+
10+
public Payment(String type) {
11+
this.type = type;
12+
}
13+
14+
public String getType() {
15+
return type;
16+
}
17+
18+
public void setType(String type) {
19+
this.type = type;
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.factory;
2+
3+
import com.baeldung.dto.PaymentDto;
4+
5+
public class PaymentDtoFactory {
6+
7+
public static String CASH = "Cash";
8+
public static String CARD = "Card";
9+
10+
public static PaymentDto cash() {
11+
return new PaymentDto(CASH);
12+
}
13+
14+
public static PaymentDto card() {
15+
return new PaymentDto(CARD);
16+
}
17+
18+
public static PaymentDto defaultPaymentDto() {
19+
return cash();
20+
}
21+
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.mapper;
2+
3+
import com.baeldung.dto.AncestorDto;
4+
import com.baeldung.entity.GrandDad;
5+
import org.mapstruct.Mapper;
6+
import org.mapstruct.Mapping;
7+
8+
@Mapper
9+
public interface AncestorMapper {
10+
11+
@Mapping(source = "identity.name", target = "firstName")
12+
@Mapping(source = "weight", target = "weight", numberFormat = "###.#")
13+
AncestorDto toDto(GrandDad grandDad);
14+
15+
}

mapstruct-3/src/main/java/com/baeldung/mapper/MediaMapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.baeldung.mapper;
22

3-
import org.mapstruct.Mapper;
4-
53
import com.baeldung.dto.MediaDto;
64
import com.baeldung.entity.Media;
5+
import org.mapstruct.Mapper;
76

87
@Mapper
98
public interface MediaMapper {

0 commit comments

Comments
 (0)