mapstruct / mapstruct/mapstruct-examples
Add example for SolrDocument + Mapstruct
还没有人认领这个 Issue。
- 主要语言
- Java
- 星标
- 1.4k
- 派生
- 504
- PR 合并指标
- 30 天内没有已合并 PR
描述
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](https://github.com/apache/lucene-solr/blob/master/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java) implements Iterable interface and Target DTOs doesn't .
So i came up with below solution to deal with this.
**Source Class**
[SolrDocument](https://github.com/apache/lucene-solr/blob/master/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java)
Here SolrDocument internally has Map _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.
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先检查仓库中现有的 Java 示例布局,然后以 issue 中关于 SolrDocument、SolrResponseWrapper、ItemDTO 和 ItemMapper 的示例为起点。添加一个专门展示基于 wrapper 的映射的示例,并验证该示例确实演示了所报告的 iterable-to-non-iterable 问题及其解决方案。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- java
- 领域
- documentation
- Issue 类型
- 文档
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100