Add example for SolrDocument + Mapstruct
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 35/100
- Issue 类型
- 文档
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- java
调研方向
首先检查仓库中现有的 Java 示例布局,然后以 issue 中关于 SolrDocument、SolrResponseWrapper、ItemDTO 和 ItemMapper 的示例为起点。添加一个专门展示基于 wrapper 的映射的示例,并验证该示例确实演示了所报告的 iterable-to-non-iterable 问题及其解决方案。
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- Java
- 星标
- 1.4k
- 派生
- 504
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
mapstruct/mapstruct-examples 的其他 Issue
-
Java 21 support 未关闭
难度 4/5 3-5 天 新手友好度 35/100
mapstruct/mapstruct-examples#153 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 25/100
mapstruct/mapstruct-examples#149 · 1 条评论 ·
-
mapstruct-protobuf3 use lombok:UnsupportedOperationException(Collections$UnmodifiableCollection) 未关闭
难度 2/5 1-3 小时 新手友好度 45/100
mapstruct/mapstruct-examples#142 ·
-
难度 4/5 3-5 天 新手友好度 15/100
mapstruct/mapstruct-examples#138 ·
-
难度 4/5 3-5 天 新手友好度 25/100
mapstruct/mapstruct-examples#128 · 1 个 reaction ·
查看 mapstruct/mapstruct-examples 的全部 Issue
相似的 Issue
-
bug untriaged
难度 2/5 1-3 小时 新手友好度 84/100
opensearch-project/ml-commons#5094 ·
-
bug
难度 2/5 1-3 小时 新手友好度 85/100
-
emitter:client:csharp feature
难度 2/5 1-3 小时 新手友好度 72/100
-
affects/8.10 affects/8.9 component/clients kind/bug likelihood/mid severity/mid
难度 2/5 1-3 小时 新手友好度 78/100
-
bug frontend maui-pilot
难度 2/5 1-3 小时 新手友好度 72/100