Passing context information to mapping method

In this post under MapStruct, I will show with example how to pass context information to the mapping methods.

For our example, I will have “Person” entity and map it to “PersonDTO” entity.

Below is the class structure of “Person” class

Person

package package34;
public class Person {
private int id;
private String name;
private Address address;
//Removed getter and setter for brevity
}

Below is the class structure of “Address”, which is referred by “Person” class

Address

package package34;
public class Address {
private String addressLine1;
private String addressLine2;
//Removed the getter and setter for brevity
}

Below is the class structure for “PersonDTO”

PersonDTO

package package34;
public class PersonDTO {
private int id;
private String name;
private AddressDTO addressDTO;
//Removed the getter, setter, and toString for brevity
}

Below is the class structure for “AddressDTO”, which is referred by “PersonDTO” class

AddressDTO

package package34;
public class AddressDTO {
private String addressLine1;
private String addressLine2;
private String country;
//Removed the getter, setter, and toString for brevity
}

Below is the Mapper interface that maps “Person” to “PersonDTO”

PersonMapper

package package34;
import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import java.util.Locale;
@Mapper
public interface PersonMapper {
@Mapping(target = "addressDTO", source = "address")
@Mapping(target = "addressDTO.country", expression = "java(locale.getCountry())")
PersonDTO personToPersonDTO(Person person, @Context Locale locale);
}

In the above interface mapping method “personToPersonDTO”, I am passing “Locale” instance as context information and I have annotated it with “@Context” annotation.

This “@Context” annotation tells MapStruct framework that it is context information and it should have to passed as arguments to subsequent methods called inside the mapping method.

So the MapStruct framework will generate the below implementation for the Mapper interface

package package34;
import java.util.Locale;
/*
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2026-01-31T10:02:53+0530",
comments = "version: 1.5.5.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-8.5.jar, environment: Java 21.0.4 (Amazon.com Inc.)"
)
*/
public class PersonMapperImpl implements PersonMapper {
@Override
public PersonDTO personToPersonDTO(Person person, Locale locale) {
... //Removed code for brevity
personDTO.setAddressDTO( addressToAddressDTO( person.getAddress(), locale ) );
... //Removed code for brevity
return personDTO;
}
protected AddressDTO addressToAddressDTO(Address address, Locale locale) {
... //Removed code for brevity
addressDTO.setCountry( locale.getCountry() );
return addressDTO;
}
}

As you see in the above generated code “personToPersonDTO” method receives the “Locale” instance as context information.

Since MapStruct has to map “Address” to “AddressDTO”, it also generates another mapping method “addressToAddressDTO” and it is also passing “Locale” instance as context information to that method also.

In this way we can pass context information to mapping methods.

Below is the Main class showing usage of “PersonMapper” interface.

Main class

package package34;
import org.mapstruct.factory.Mappers;
import java.util.Locale;
public class Example34 {
public static void main(String[] args) {
PersonMapper personMapper = Mappers.getMapper(PersonMapper.class);
Address address = new Address();
address.setAddressLine1("addressLine1");
address.setAddressLine2("addressLine2");
Person person = new Person();
person.setId(1);
person.setName("John Doe");
person.setAddress(address);
PersonDTO personDTO = personMapper.personToPersonDTO(person, Locale.US);
System.out.println(personDTO);
}
}

Below is the output

Output

PersonDTO{id=1, name='John Doe', addressDTO=AddressDTO{addressLine1='addressLine1', addressLine2='addressLine2', country='US'}}

Leave a comment