P
P
Pavel Talaiko2018-10-31 11:05:47
Java
Pavel Talaiko, 2018-10-31 11:05:47

How to properly use mapstruct with mapping parameters?

Hello.
So the situation is this.

//Есть сущность
public class MyObject {
  public Word word; // имеет множество переводов, содержит в себе
}

//и необходимо смапить в сущность с переводом

public class MyObjectDto {
  public String wordValue; // имеет множество переводов, содержит в себе
}

@Component
public class CustomMapper {
  public String converterMapper(Word word, Language lang)
}

@Mapper(componentModel = "spring", uses = CustomMapper.class)
public interface MyObjectMapper {
  //И почему не всегда @Autowired CustomMapper из-за этого не могу использовать expression
@Mappings({
            @Mapping(source = "word", target = "wordValue", qualifiedBy=(CustomMapper.class???)) //как передать параметр
    });
    MyObjectDto map(MyObject mo); 

    MyObject map(MyObjectDto myObjectDto);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Talaiko, 2018-12-19
@jsdevel

So that's how it was done.

@Mapper(componentModel = "spring", uses = CustomMapper.class)
public interface MyObjectMapper {
        @Mappings({
            @Mapping(target = "field", expression = "java(customMapper.convert(obj.field, lang))"
         })
         DTO map(Entity obj, Language lang);
}

If you use Spring, then you need to not only specify , but also remove the field that has INSTANCE, otherwise @Autowired will not substitute the implementation.componentModel = "spring"
...
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
...

I think the topic can be closed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question