Add example for SolrDocument + Mapstruct
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- java
- Domain
- documentation
Research direction
Start by reviewing existing Java example layouts in the repository, then use the issue's SolrDocument, SolrResponseWrapper, ItemDTO, and ItemMapper example as the starting point. Add a focused example showing the wrapper-based mapping and verify that the example demonstrates the reported iterable-to-non-iterable problem and its resolution.
Written by the indexing model from the issue text.
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.
- Dominant language
- Java
- Stars
- 1.4k
- Forks
- 504
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from mapstruct/mapstruct-examples
-
Java 21 support Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
mapstruct/mapstruct-examples#153 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
mapstruct/mapstruct-examples#149 · 1 comment ·
-
mapstruct-protobuf3 use lombok:UnsupportedOperationException(Collections$UnmodifiableCollection) Open
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
mapstruct/mapstruct-examples#142 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 15/100
mapstruct/mapstruct-examples#138 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
mapstruct/mapstruct-examples#128 · 1 reaction ·
All issues in mapstruct/mapstruct-examples
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
-
Two open-case totals on one screen: the Programs tile says 15,858 and the nav badge says 15,868 Openbug frontend maui-pilot
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
objectionary/eo-graphs#74 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100