Passing context information to custom mapping method

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

In the previous post, I have explained how to tell MapStruct which is the context information i.e., by annotating it with “@Context” annotation as shown below

@Mapping(target = "addressDTO", source = "address")
PersonDTO personToPersonDTO(Person person, @Context Locale locale);

Now in this post, for our example, I will map “Person” instance to “PersonDTO” instance.

Below is the class structure of “Person”

Person

package package35;
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”

Address

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

Below is the class structure of “PersonDTO”

PersonDTO

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

Below is the class structure of “AddressDTO”

AddressDTO

package package35;
public class AddressDTO {
private String addressLine;
private String country;
//Removed getter, setter, and toString for brevity
}

Now to map “Person” to “PersonDTO and “Address” to “AddressDTO” I will create the mapper interface as shown below

PersonMapper

package package35;
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")
PersonDTO personToPersonDTO(Person person, @Context Locale locale);
public default AddressDTO addressToAddressDTO(Address address, @Context Locale locale) {
AddressDTO addressDTO = new AddressDTO();
addressDTO.setAddressLine(address.getAddressLine1() + ", " + address.getAddressLine2());
addressDTO.setCountry(locale.getCountry());
return addressDTO;
}
}

As you can see in the above code, I have created a custom mapping method specifically for “Address” to “AddressDTO” mapping.

I am also telling the MapStruct that when calling “addressToAddressDTO” method pass the Locale information as context information to this method. Note I have annotated the “Locale” parameter with “@Context” annotation.

Below is the implementation class generated by MapStruct

PersonMapperImpl

package package35;
import java.util.Locale;
/*
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2026-02-21T09:51:08+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) {
if ( person == null ) {
return null;
}
PersonDTO personDTO = new PersonDTO();
personDTO.setAddressDTO( addressToAddressDTO( person.getAddress(), locale ) );
personDTO.setId( person.getId() );
personDTO.setName( person.getName() );
return personDTO;
}
}

As you can see in the above code, at line 22, when calling custom mapping method “addressToAddressDTO”, the locale information is also passed as context information.

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

Below is the main class for your reference.

Main class

package package35;
import org.mapstruct.factory.Mappers;
import java.util.Locale;
public class Example35 {
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{addressLine='addressLine1, addressLine2', country='US'}}

Leave a comment