MapStruct - Using Default Expression



Using Mapstruct we can pass a computed value using defaultExpression in case source property is null using defaultExpression attribute of @Mapping annotation.

Syntax

@Mapping(target = "target-property", source="source-property" defaultExpression = "default-value-method")

Here

  • default-value-method − target-property will be set as result of default-value-method in case source-property is null.

Following example demonstrates the same.

Example - Using Default Expression

Open project mapping as updated in Mapping Using defaultValue chapter in Eclipse.

Update CarEntity.java with following code −

CarEntity.java

package com.tutorialspoint.entity; import java.util.GregorianCalendar; public class CarEntity { private int id; private double price; private GregorianCalendar manufacturingDate; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public GregorianCalendar getManufacturingDate() { return manufacturingDate; } public void setManufacturingDate(GregorianCalendar manufacturingDate) { this.manufacturingDate = manufacturingDate; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

Update Car.java with following code −

Car.java

package com.tutorialspoint.model; public class Car { private int id; private String price; private String manufacturingDate; private String brand; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getManufacturingDate() { return manufacturingDate; } public void setManufacturingDate(String manufacturingDate) { this.manufacturingDate = manufacturingDate; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

Update CarMapper.java with following code −

CarMapper.java

package com.tutorialspoint.mapper; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import com.tutorialspoint.entity.CarEntity; import com.tutorialspoint.model.Car; import java.util.UUID; @Mapper( imports = UUID.class ) public interface CarMapper { @Mapping(source = "name", target = "name", defaultExpression = "java(UUID.randomUUID().toString())") @Mapping(target = "brand", constant = "BMW") @Mapping(source = "price", target = "price", numberFormat = "$#.00") @Mapping(source = "manufacturingDate", target = "manufacturingDate", dateFormat = "dd.MM.yyyy") Car getModelFromEntity(CarEntity carEntity); }

Update CarMapperTest.java with following code −

CarMapperTest.java

package com.tutorialspoint.test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.GregorianCalendar; import org.junit.jupiter.api.Test; import org.mapstruct.factory.Mappers; import com.tutorialspoint.entity.CarEntity; import com.tutorialspoint.mapper.CarMapper; import com.tutorialspoint.model.Car; public class CarMapperTest { private CarMapper carMapper=Mappers.getMapper(CarMapper.class); @Test public void testEntityToModel() { CarEntity entity = new CarEntity(); entity.setPrice(345000); entity.setId(1); entity.setManufacturingDate(new GregorianCalendar(2015, 3, 5)); Car model = carMapper.getModelFromEntity(entity); assertEquals(model.getPrice(), "$345000.00"); assertEquals(entity.getId(), model.getId()); assertEquals("05.04.2015", model.getManufacturingDate()); assertNotNull(model.getName()); assertEquals("BMW", model.getBrand()); } }

Run the following command to test the mappings.

mvn clean test

Output

Once command is successful. Verify the output.

mvn clean test
[INFO] Scanning for projects...
...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.tutorialspoint.test.[1mCarMapperTest[m
[INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.092 s -- in com.tutorialspoint.test.[1mCarMapperTest[m
[INFO] Running com.tutorialspoint.test.[1mDeliveryAddressMapperTest[m
[INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 s -- in com.tutorialspoint.test.[1mDeliveryAddressMapperTest[m
[INFO] Running com.tutorialspoint.test.[1mStudentMapperTest[m
[INFO] [1;32mTests run: [0;1;32m2[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 s -- in com.tutorialspoint.test.[1mStudentMapperTest[m
[INFO] 
[INFO] Results:
[INFO] 
[INFO] [1;32mTests run: 4, Failures: 0, Errors: 0, Skipped: 0[m
[INFO] 
[INFO] [1m------------------------------------------------------------------------[m
[INFO] [1;32mBUILD SUCCESS[m
[INFO] [1m------------------------------------------------------------------------[m
[INFO] Total time:  3.968 s
[INFO] Finished at: 2025-09-14T15:56:57+05:30
[INFO] [1m------------------------------------------------------------------------[m
Advertisements