Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for SolrDocument + Mapstruct #68

Open
vikky2101 opened this issue Jun 11, 2019 · 0 comments
Open

Add example for SolrDocument + Mapstruct #68

vikky2101 opened this issue Jun 11, 2019 · 0 comments

Comments

@vikky2101
Copy link

vikky2101 commented Jun 11, 2019

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.

@vikky2101 vikky2101 changed the title Add example for SolrDocument = Add example for SolrDocument + Mapstruct Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant