mapstruct / mapstruct/mapstruct-examples
Add example for SolrDocument + Mapstruct
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Java
- Sterne
- 1.4k
- Forks
- 504
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
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.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die vorhandenen Layouts für Java-Beispiele im Repository zu prüfen, und verwende dann das SolrDocument-, SolrResponseWrapper-, ItemDTO- und ItemMapper-Beispiel aus dem Issue als Ausgangspunkt. Füge ein fokussiertes Beispiel hinzu, das das wrapperbasierte Mapping zeigt, und verifiziere, dass das Beispiel das gemeldete iterable-to-non-iterable-Problem und seine Lösung demonstriert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- java
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100