--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-use...@googlegroups.com.
To post to this group, send email to mapstru...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
"
In addition to methods defined on the same mapper type MapStruct can also invoke mapping methods defined in other classes, be it mappers generated by MapStruct or hand-written mapping methods. This can be useful to structure your mapping code in several classes (e.g. with on mapper type per application module) or you want to provide custom mapping logic which can’t be generated by MapStruct.
For instance the Car class might contain an attribute manufacturingDate while the corresponding DTO attribute is of type String. In order to map this attribute, you could implement a mapper class like this:
- public class DateMapper {
- public String asString(Date date) {
- return date != null ? new SimpleDateFormat( "yyyy-MM-dd" )
- .format( date ) : null;
- }
- public Date asDate(String date) {
- try {
- return date != null ? new SimpleDateFormat( "yyyy-MM-dd" )
- .parse( date ) : null;
- }
- catch ( ParseException e ) {
- throw new RuntimeException( e );
- }
In the @Mapper annotation at the CarMapper interface reference the DateMapper class like this:
- @Mapper(uses=DateMapper.class)
- public class CarMapper {
- CarDto carToCarDto(Car car);
- }
When generating code for the implementation of the carToCarDto() method, MapStruct will look for a method which maps a Date object into a String, find it on the DateMapper class and generate an invocation ofasString() for mapping the manufacturingDate attribute.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-users+unsubscribe@googlegroups.com.
To post to this group, send email to mapstruct-users@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-use...@googlegroups.com.
To post to this group, send email to mapstru...@googlegroups.com.