Issue
I'm new at MapStruct.
I have a Word object that contains a string value and a set of itself and I want to map it to WordDTO that contains a value and set of string values. and I have no idea how to do that.
As I said in the comments it makes sense that mapstruct can't map two objects but if it helps I put the error below:
Error:(20,13) java: Can't map property "java.util.Set<Word> synonyms" to "java.util.Set<java.lang.String> synonyms". Consider to declare/implement a mapping method: "java.util.Set<java.lang.String> map(java.util.Set<Word> value)"
Word class:
public class Word {
private String wordValue;
private Set<Word> synonyms;
}
WordDTO class:
public class WordDTO {
private String wordValue;
private Set<String> synonyms;
}
(actually set or list... it doesn't matter at all.)
I implemented this interface for mapping:
@Mapper
public interface WordMapper {
WordMapper INSTANCE= Mappers.getMapper(WordMapper.class);
WordDTO wordToWordDTO(Word word);
Word wordDTOToWord(WordDTO wordDTO);
}
thank you for your helping.
Solution
Mapstruct is complaining that he cannot convert Word
to String
. You have to tell explicitly the mapper how to do it.
Here is an example:
@Mapper
public interface WordMapper {
WordMapper INSTANCE= Mappers.getMapper(WordMapper.class);
WordDTO wordToWordDTO(Word word);
Word wordDTOToWord(WordDTO wordDTO);
// Map String to Word
@Mapping(source = "value", target = "wordValue")
Word stringToWord(String value)
// Map Word to string
default String wordToString(Word word) {
return (word == null) ? null : word.getWordValue();
}
}
Answered By - Thierno Barry