Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much...
Transcript of Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much...
![Page 1: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/1.jpg)
Easy Bean Mappings with MapStruct 1.2
19 June 2017
Thomas Much @thmuch
1100 1010 1111 1110 1011 1010 1011 1110
![Page 2: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/2.jpg)
Who's talking?
• Thomas Much
• Agile Developer Coach & XP Coder (Java et al.)
• Likes to use cool little tools & libraries (and tell other people about them)
www.jughh.de
![Page 3: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/3.jpg)
Java & beans, some history1995
2015
1997
JavaBeans
1998
FormBeans
1999
DTOs
2000
POJOs
2005
JAXBEntities
2006
JPAEntities
Type safe,fast
bean mappings!
![Page 4: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/4.jpg)
System architecture, old style
![Page 5: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/5.jpg)
The promise of Java EE 5+ & Co.
![Page 6: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/6.jpg)
Real Life
![Page 7: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/7.jpg)
Real Life!
Bounded Context
A
DomainModel A
Transport Model A
Bounded Context
B
DomainModel B
![Page 8: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/8.jpg)
The necessary mappings…@Entity @Table(name = "CUSTOMERS") public class Customer {
@Id Long id;
long customerId;
String name;
@Enumerated Title title;
… getters and setters! … }
public class CustomerDTO {
long id;
String customerId;
String name;
String title;
… getters and setters! … }
![Page 9: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/9.jpg)
… can be quite tedious
JPA Entity
DTO
JAXB Entity
REST Entity
View Bean
![Page 10: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/10.jpg)
Mapping implementation – how?• DIY
• Call getters / setters manually
• Use own reflection library
• Reflection-based mapping libraries:
• Apache BeanUtils, Dozer, …
• Problems: Manual effort, missing type safety, performance …
![Page 11: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/11.jpg)
Performance… Performance!
https://twitter.com/joao_b_reis/status/559780053979250688
![Page 12: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/12.jpg)
• Annotation processor, generates mapping (source) code.
• No reflection!
• Type safe and fast.
• Requires at least Java 6, special support for Java 8.
• Small runtime dependency (< 20 K) or none at all (depending on the component model).
![Page 13: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/13.jpg)
Versions
• MapStruct 1.0.Final released in 2015.
• MapStruct 1.1: Rock solid. Used in production.
• MapStruct 1.2: Beta.
![Page 14: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/14.jpg)
JARs (Dependencies)
http://mvnrepository.com/artifact/org.mapstruct
Java 8+
Java 6+, not needed at runtime
Java 6 / 7
![Page 15: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/15.jpg)
Example – map JPA entity to DTO@Entity @Table(name = "CUSTOMERS") public class Customer {
@Id Long id;
long customerId;
String name;
@Enumerated Title title;
… getters and setters! … }
public class CustomerDTO {
long id;
String customerId;
String name;
String title;
… getters and setters! … }
Our task: Map all public properties (including superclasses).
![Page 16: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/16.jpg)
Make a wish!
public interface CustomerMapper {
CustomerDTO customer2DTO(Customer customer);
}
![Page 17: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/17.jpg)
Make a wish!@Mapper(componentModel = "cdi") public interface CustomerMapper {
CustomerDTO customer2DTO(Customer customer);
} @Inject private CustomerMapper mapper; …
CustomerDTO dto = mapper.customer2DTO( customer );
![Page 18: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/18.jpg)
Generate the implementation
Live
Demo
• How do we generate the implementation for the interface?
• Press "Save" in your IDE.
• More precisely: Run the compiler (e.g. javac) …
• … and let the annotation processor kick in.
![Page 19: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/19.jpg)
@Generated( value = "org.mapstruct.ap.MappingProcessor", date = "2017-06-15T16:41:17+0200", comments = "version: 1.2.0.Beta3, compiler: Eclipse JDT (IDE) ..." ) @ApplicationScoped public class CustomerMapperImpl implements CustomerMapper {
@Override public CustomerDTO customer2DTO(Customer customer) {
if (customer == null) { return null; }
CustomerDTO customerDTO = new CustomerDTO(); customerDTO.setCustomerId(String.valueOf(customer.getCustomerId()));
if (customer.getId() != null) { customerDTO.setId(String.valueOf(customer.getId())); }
customerDTO.setName(customer.getName());
if (customer.getTitle() != null) { customerDTO.setTitle(customer.getTitle().name()); }
return customerDTO; } }
Generated source code
Thread-safe.
Clean. Easy to read.
@Generated attributes can be turned off.
![Page 20: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/20.jpg)
No container? No problem!@Mapper public interface CustomerMapper {
CustomerMapper INSTANCE = Mappers.getMapper( CustomerMapper.class );
CustomerDTO customer2DTO(Customer customer);
}
CustomerDTO dto = CustomerMapper.INSTANCE.customer2DTO( customer );
![Page 21: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/21.jpg)
Type (& value) safety
WARN / ERROR / IGNORE
set policy globally or per mapper (or use default)
![Page 22: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/22.jpg)
Basic mappings@Mapper public interface CustomerMapper {
@Mappings({ @Mapping(target="custNo", source="customerId"), @Mapping(target="password", ignore=true), @Mapping(target="lastLogin", dateFormat="dd.MM.yyyy") }) CustomerDTO customer2DTO(Customer customer);
}
![Page 23: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/23.jpg)
Mapping enum values@Mapper public interface CustomerMapper {
@ValueMapping(target="HR", source="MR") @ValueMapping(target="FRU", source="MRS") @ValueMapping(target="FRK", source="MS") @ValueMapping(target="<NULL>", source="<ANY_UNMAPPED>") DtoTitle title2DtoTitle(Title title);
CustomerDTO customer2DTO(Customer customer);
}See MappingConstants
![Page 24: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/24.jpg)
Lots of implicit type conversions
primitives ⬌ wrappers
number types (incl. Big…) ⬌ other number types & precisions
String ⬌ nearly everything (incl. enums & date/number formats)
Date/Calendar ⬌ Joda ⬌ Java 8 date/time
JAXB ⬌ elements, collections
![Page 25: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/25.jpg)
Object references@Mapper public interface CustomerMapper {
CustomerDTO customer2DTO(Customer customer); }
@Entity public class Customer {
Address address;
… other properties … … getters and setters! … }
That's all! 😀
![Page 26: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/26.jpg)
Customizing reference mapping@Mapper(disableSubMappingMethodsGeneration=true) public interface CustomerMapper {
CustomerDTO customer2DTO(Customer customer);
AddressDTO address2DTO(Address address);
}
![Page 27: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/27.jpg)
Using other mappers@Mapper(uses=AddressMapper.class) public interface CustomerMapper {
CustomerDTO customer2DTO(Customer customer); }
@Mapper public interface AddressMapper {
AddressDTO address2DTO(Address address); }
![Page 28: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/28.jpg)
Custom mapping implementation@Mapper public interface CustomerMapper {
CustomerDTO customer2DTO(Customer customer);
default AddressDTO address2DTO(Address address) { AdressDTO dto = new AddressDTO(); … custom mapping code … return dto; } } Or use an abstract
class instead.
![Page 29: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/29.jpg)
Use your existing mapping code@Mapper(uses=LegacyAddressMapper.class) public interface CustomerMapper { CustomerDTO customer2DTO(Customer customer); }
public class LegacyAddressMapper { public AddressDTO address2DTO(Address address) { … custom mapping code … return dto; } }
Provide default constructor or static mapping method.
![Page 30: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/30.jpg)
Multiple source parameters@Mapper public interface PrintLabelMapper {
@Mapping(target="name", source="customer.name") @Mapping(target="street", source="address.street") @Mapping(target="city", source="address.city") PrintLabelDTO customerAddress2DTO( Customer customer, Address address); }
Path expressions can generally be used in "source" and "target".
![Page 31: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/31.jpg)
Updates & reverse mappings@Mapper public interface CustomerMapper {
CustomerDTO customer2DTO(Customer customer);
void updateCustomerDTO(Customer customer, @MappingTarget CustomerDTO dto);
Customer dto2Customer(CustomerDTO dto); }
May return target (for fluent API). Max. 1 @MappingTarget!
![Page 32: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/32.jpg)
Reuse mapping configurations@Mapper public interface CustomerMapper {
@Mapping( … ) CustomerDTO customer2DTO(Customer customer);
@InheritConfiguration(name="customer2DTO") void updateCustomerDTO(Customer customer, @MappingTarget CustomerDTO dto);
@InheritInverseConfiguration(name="customer2DTO") Customer dto2Customer(CustomerDTO dto); }
![Page 33: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/33.jpg)
Context parameters@Mapper public interface CustomerMapper {
CustomerDTO customer2DTO(Customer customer, @Context Locale locale);
AddressDTO address2DTO(Address address, @Context Locale locale); }
Will be passed in generated implementation
Client call
![Page 34: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/34.jpg)
Target type in custom mappers@ApplicationScoped public class JpaEntityMapper {
@PersistenceContext private EntityManager manager;
public <T extends BaseEntity> T resolve(Long id, @TargetType Class<T> entityClass) { return (id != null) ? manager.find(entityClass, id) : null; } }
![Page 35: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/35.jpg)
Object factoriespublic class DtoFactory { public CustomerDTO createCustomerDTO() { return new CustomerDTO(); } }
public class EntityFactory { public <T extends BaseEntity> T createEntity(@TargetType Class<T> entityClass) { return entityClass.newInstance(); } }
![Page 36: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/36.jpg)
Streams, collections … of beans@Mapper public interface CustomerMapper {
CustomerDTO customer2DTO(Customer customer);
List<CustomerDTO> list2List(List<Customer> customer); List<CustomerDTO> array2List(Customer[] customer); CustomerDTO[] set2Array(Set<Customer> customer);
List<CustomerDTO> stream2List(Stream<Customer> cust); Stream<CustomerDTO> set2Stream(Set<Customer> cust); }
![Page 37: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/37.jpg)
Streams, collections … of objects@Mapper public interface StreamCollectionMapper {
Set<Long> int2Long(Stream<Integer> stream); Stream<Long> int2Long(List<Integer> list);
@IterableMapping(dateFormat="dd.MM.yyyy") List<String> date2String(List<Date> list);
@MapMapping(valueDateFormat="dd.MM.yyyy") Map<String,String> map2map(Map<Long,Date> map); }
![Page 38: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/38.jpg)
Exceptions
Every checked exception
not declared in the mapping method's signature
will be rethrown as a RuntimeException.
![Page 39: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/39.jpg)
Customizing around invocation
• Callbacks
• @BeforeMapping, @AfterMapping
• Decorators
• @DecoratedWith built-in – but better use CDI decorators.
![Page 40: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/40.jpg)
MapStruct SPI
• Customize accessor naming strategy
• e.g. fluent API instead of get/set
• Mapping exclusions
• Exclude certain types from automatic method generation
![Page 41: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/41.jpg)
There's a lot more …• Constant values & Java expressions as "source".
• Default values, if source is null.
• Order of setter invocation ("dependsOn").
• Customized null checks & default null values.
• Centralized mapping configurations ("config", @MapperConfig).
• Selection of ambiguous mapping methods by qualifier or name.
![Page 42: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/42.jpg)
Build tool integration• javac
• SPI – just put MapStruct on the classpath!
• Maven, Ant, Gradle
• See documentation for (easy) setup instructions:
• Add MapStruct dependency, setup annotation processor.
![Page 43: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/43.jpg)
Eclipse plug-in• Code completion
• Quick fixes
• https://github.com/mapstruct/mapstruct-eclipse
![Page 44: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/44.jpg)
New in MapStruct 1.2
• Support for Java 8 streams.
• Mappings based on public fields.
• Automatic generation of sub-mapping methods.
• Integration with Project Lombok.
• Support for Java 9 (experimental).
![Page 45: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/45.jpg)
MapStruct – Recap• Meets current, real-world bean mapping needs.
• Generated code is fast, clean and easy to read.
• Flexible mappings, built-in and custom.
• Easy integration with different component models.
• Extensive and well-written documentation.
![Page 46: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/46.jpg)
Curious? Intrigued? Questions?
http://mapstruct.org/https://github.com/mapstruct/mapstruct-examples
😃
![Page 47: Easy Bean Mappings with MapStruct 1Easy Bean Mappings with MapStruct 1.2 19 June 2017 Thomas Much @thmuch 1100 1010 1111 1110 1011 1010 1011 1110 Java & beans, some history 1995 1997](https://reader034.fdocuments.us/reader034/viewer/2022050312/5f74cbaad36fe52e8447adad/html5/thumbnails/47.jpg)
Thank you: