- MapStruct - Home
- MapStruct - Overview
- MapStruct - Environment Setup
- MapStruct - Mapping
- MapStruct - Basic Mapping
- MapStruct - Custom Mapping
- MapStruct - Mapping Multiple
- MapStruct - Mapping Nested Bean
- MapStruct - Mapping Direct Field
- MapStruct - Builder
- MapStruct - Data Type Conversions
- MapStruct - Implicit Type Conversion
- MapStruct - Using numberFormat
- MapStruct - Using dateFormat
- MapStruct - Using expression
- MapStruct - Using constant
- MapStruct - Using defaultValue
- MapStruct - Using defaultExpression
- MapStruct - Mapping Collections
- MapStruct - Mapping List
- MapStruct - Mapping Map
- Mapstruct - Miscellaneous
- MapStruct - Mapping Streams
- MapStruct - Mapping Enum
- MapStruct - Throwing Exceptions
- MapStruct - Customizing Mapper
- MapStruct - Useful Resources
- MapStruct - Quick Guide
- MapStruct - Useful Resources
- MapStruct - Discussion
MapStruct - Using expression
MapStruct allows to call a conversion method for customized logic. We can use expression to achieve the same where we can pass any java object and call its method to do the conversion.
Syntax
@Mapping(target = "target-property", expression = "java(target-method())")
Here
target-property − the property for which we are doing the mapping.
expression − mapper will call the java method written in the expression.
target-method − target-method is the method to be called. In case method is present in different class, use new class-name.target-method()
Example - Using expression
Open project mapping as updated in Mapping Using dateFormat 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;
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;
}
}
Update Car.java with following code −
Car.java
package com.tutorialspoint.model;
public class Car {
private int id;
private String price;
private String manufacturingDate;
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;
}
}
Update CarMapper.java with following code −
CarMapper.java
package com.tutorialspoint.mapper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import com.tutorialspoint.entity.CarEntity;
import com.tutorialspoint.model.Car;
@Mapper
public interface CarMapper {
@Mapping(source = "price", target = "price", numberFormat = "$#.00")
@Mapping(target = "manufacturingDate",
expression = "java(getManufacturingDate(carEntity.getManufacturingDate()))")
Car getModelFromEntity(CarEntity carEntity);
default String getManufacturingDate(GregorianCalendar manufacturingDate) {
Date d = manufacturingDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy" );
return sdf.format( d );
}
}
Update CarMapperTest.java with following code −
CarMapperTest.java
package com.tutorialspoint.test;
import static org.junit.jupiter.api.Assertions.assertEquals;
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());
}
}
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.037 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.886 s [INFO] Finished at: 2025-09-14T12:05:42+05:30 [INFO] [1m------------------------------------------------------------------------[m