簡體   English   中英

MapStruct-如何指定屬性的通用類型?

[英]MapStruct - How to specify generic type of an attribute?

Mapstruct找不到屬性的通用類型。 讓我們以一個例子來闡明我要做什么。

考慮以下dto:

class ListForm<T> {
 private Collection<T> adds;
 private Collection<T> changes;
 private Collection<T> deletes;
}

class Person {
 private String name;
}

class PersonDto { 
 private String name;
}

我嘗試實現以下映射器:

@Mapper
public interface OccupantMapper {
    ListForm<Person> test(ListForm<PersonDto> person);

    Collection<Person> toPersons (Collection<PersonDto> persons);
}

但是,這是mapstruct生成的一部分:

ListForm<Person> listForm= new ListForm<Person>();
if ( occ.getAjouts() != null ) {
        if ( listForm.getAjouts() != null ) {
            // problem here, mapstruct can't find the type of the attribute
            Collection<T> targetCollection = person.getAdds();
            if ( targetCollection != null ) {
                listForm.getAjouts().addAll( targetCollection );
            }
        }
    }

如您在下面的代碼中看到的,mapstruct找不到目標集合的類型。 並且它不會將PersonD的列表轉換為Person的列表。 這是mapstruct應該生成的。

Collection<Occupant> targetCollection = toPersons(person.getAdds());

你能告訴我這是不是蟲子? 如果有解決辦法? 還是我應該另辟do徑? ks

您是否可以自己從最新的master( https://github.com/mapstruct/mapstruct )構建MapStruct,看看是否能為您解決問題? 我上周修復了一個相關的錯誤,我認為與您的問題基本上相同。

您只需要運行“ mvn clean install”並在項目中引用依賴項的SNAPSHOT版本。

已在MapStruct 1.1.0.Beta1版本中更正此錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM