mapstruct / mapstruct/mapstruct-examples

Add example for SolrDocument + Mapstruct

未關閉
#68 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 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 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.

貢獻指南

這個儲存庫沒有索引到貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先檢查儲存庫中現有的 Java 範例配置,然後以 issue 中關於 SolrDocument、SolrResponseWrapper、ItemDTO 和 ItemMapper 的範例作為起點。加入一個專門展示基於 wrapper 的 mapping 範例,並確認該範例確實演示了所回報的 iterable-to-non-iterable 問題及其解決方案。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
java
領域
documentation
Issue 類型
文件
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。