mapstruct / mapstruct/mapstruct-examples
Add example for SolrDocument + Mapstruct
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Java
- Star
- 1.4k
- Fork
- 504
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
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.
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách xem xét các bố cục ví dụ Java hiện có trong repository, sau đó sử dụng ví dụ trong issue về SolrDocument, SolrResponseWrapper, ItemDTO và ItemMapper làm điểm khởi đầu. Thêm một ví dụ tập trung thể hiện việc mapping dựa trên wrapper và xác minh rằng ví dụ đó minh họa vấn đề iterable-to-non-iterable đã được báo cáo cùng cách giải quyết vấn đề.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- java
- Lĩnh vực
- documentation
- Loại issue
- Tài liệu
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100