In light of the Java 8 Stream APIs and with lombok
annotation processor library, I am no longer using this mapping frameworks. I create my own mappers by implementing the Converter
interface of the spring framework (package org.springframework.core.convert.converter.Converter
)
@Component
public class ProductToProductDTO implements Converter<Product, ProductDTO> {
@Override public ProductDTO convert( Product source ) {
ProductDTO to = ProductDTO.builder()
.name( source.getName())
.description( source.getDescription() )
.tags(source.getTags().stream().map(t -> t.getTag().getLabel()).collect( Collectors.toList()))
.build();
return to;
}
}
@Builder //lombok annotation to create Builder class
@Data //lombok annotation to create getters & setters
@AllArgsConstructor //required for @Builder
public class ProductDTO {
private String name;
private String description;
private List<String> tags;
}