-
Notifications
You must be signed in to change notification settings - Fork 511
Description
It would be nice to have an example that shows how to use SolrDocument with Mapstruct.
I was trying to use MapStruct with SolrDocument and couldn't find a comprehensive example that could guide me . I was facing below issue during compiling:
Can't generate mapping method from iterable type to non-iterable type.
Reason is SolrDocument implements Iterable interface and Target DTOs doesn't .
So i came up with below solution to deal with this.
Source Class
SolrDocument
Here SolrDocument internally has Map<String,Object> _fields;
Destination Class:
`public class ItemDTO {
private String id;
private String displayName;
private String availability;
private String price;
//getter
//setter
}
}`
Error : Can't generate mapping method from iterable type to non-iterable type.
Solution:
Created SolrResponseWrapper around SolrDocument like below:
`public class SolrResponseWrapper {
private SolrDocument document;
public SolrResponseWrapper(SolrDocument document) {
this.document = document;
}
public String get(String key) {
return this.document.get(key).toString();
}
}`
Now Mapper is defined below for SolResponseWrapper to ItemDTO.
`@Mapper
public interface ItemMapper {
ItemMapper INSTANCE = Mappers.getMapper(ItemMapper);
@mappings({
@mapping(expression = "java(record.get("product_id"))", target = "id"),
@mapping(expression = "java(record.get("display_name"))", target = "displayName"),
@mapping(expression = "java(record.get("availability"))", target = "availability"),
@mapping(expression = "java(record.get("price"))", target = "price")
})
ItemDTO convertToItemDTO(SolrResponseWrapper record);
}`
If you think this is a good addition, please let me know. I will raise PR to have it included as an example.