mapstruct / mapstruct/mapstruct-examples
Add example for SolrDocument + Mapstruct
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Java
- Estrellas
- 1.4k
- Forks
- 504
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
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.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza revisando los diseños existentes de ejemplos de Java en el repositorio y, después, utiliza como punto de partida el ejemplo del issue con SolrDocument, SolrResponseWrapper, ItemDTO e ItemMapper. Añade un ejemplo específico que muestre el mapeo basado en el wrapper y verifica que el ejemplo demuestre el problema iterable-to-non-iterable notificado y su resolución.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- java
- Área
- documentation
- Tipo de issue
- Documentación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100