简体   繁体   English

从arrayList检索MapStruct

[英]MapStruct Retrieving from arrayList

I'm trying to use MapStruct to map two objects. 我正在尝试使用MapStruct映射两个对象。 I have been searching for a while and I haven't been able to find anything, though I'm new to programming so I'm sure it's easier than I'm making it. 我已经搜索了一段时间,但找不到任何东西,尽管我是编程的新手,所以我确信它比我做的要容易。

Here is some stripped back code (Note that the real code is more complex, with the child object from the arraylist not being the same type as the destination objects child variable as it is here): 这是一些剥离的代码(请注意,实际代码更加复杂,arraylist中的子对象与目标对象的子变量的类型不同,这里是这样):

SourceObject SourceObject

public class SourceObject {
    public ArrayList<ListObject> list = new ArrayList<ListObject>();

    public SourceObject() {
        list.add(new ListObject());
    }
}

ListObject 的ListObject

public class ListObject {
    public DetailsObject details = new DetailsObject();

    public ListObject() {
        details.forename="SourceForename";
        details.surname="SourceSurname";
    }
}

DestinationObject DestinationObject

public class DestinationObject {
    public DetailsObject details = new DetailsObject();

    public DestinationObject() {
        details.forename="DestinationForename";
        details.surname="DestinationSurname";
    }
}

DetailsObject DetailsObject

public class DetailsObject {
    public String forename;
    public String surname;
}

Mapper 映射器

@Mappings({
    @Mapping(target="details.forename", source="list.get(0).details.forename"),
    @Mapping(target="details.surname", source="list.get(0).details.surname"),
})
DestinationObject toDestination(SourceObject source);

This will work fine if I put DetailsObject directly inside SourceObject, but becomes a problem when I try to take it from the list. 如果将DetailsObject直接放在SourceObject中,这将很好地工作,但是当我尝试从列表中获取它时,这将成为一个问题。 The error I get is: 我得到的错误是:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project template: Compilation failure: Compilation failure: [错误]无法在项目模板上执行目标org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile(默认编译):编译失败:编译失败:

[ERROR] .../src/main/java/Mapper/SourceToDestinationMap.java:[12,13] No property named "list.get(0).details.surname" exists in source parameter(s). [错误] ... / src / main / java / Mapper / SourceToDestinationMap.java:[12,13]源参数中不存在名为“ list.get(0).details.surname”的属性。 Did you mean "list.empty"? 您是说“ list.empty”吗?

[ERROR] .../src/main/java/Mapper/SourceToDestinationMap.java:[11,9] No property named "list.get(0).details.forename" exists in source parameter(s). [错误] ... / src / main / java / Mapper / SourceToDestinationMap.java:[11,9]源参数中不存在名为“ list.get(0).details.forename”的属性。 Did you mean "list.empty"? 您是说“ list.empty”吗?

EDIT: Current state of the mapper: 编辑:映射器的当前状态:

@Mapper
public interface SourceToDestinationMap {

    @Mapping(target = "details", source = "list")
    DestinationObject toDestination(SourceObject source);

    default DetailsObject map(List<ListObject> source) {
        return map(source.get(0));
    }

    DetailsObject map(ListObject source);
}

The mapping you are trying to do is not possible, since you are mixing bean properties and bean functions. 您尝试做的映射是不可能的,因为您正在混合bean属性和bean函数。

You can't use @Mapping(source = "list.get(0).XXX") . 您不能使用@Mapping(source = "list.get(0).XXX") Using indexes to access elements of a list is not yet supported see #1321 . 尚不支持使用索引访问列表的元素,请参阅#1321

A way to solve your problem would be to use Qualifiers in the same way that they are used in the mapstruct-iterable-to-non-iterable example. 解决问题的一种方法是使用限定符,就像在mapstruct-iterable-non-iterable示例中使用限定符一样。

Or you can provide your own method that would perform the mapping. 或者,您可以提供自己的执行映射的方法。

@Mapper
public interface MyMapper {

    @Mapping(target = "details", source = "list")
    DestinationObject toDestination(SourceObject source);

    default DetailsObject map(List<ListObject> source) {
        return source != null && !source.isEmpty() ? map(source.get(0)) : null;
    }

    DetailsObject map(ListObject source);
}

MapStruct will then generate the correct code. 然后,MapStruct将生成正确的代码。

Another alternative would be to use @Mapping(expression="java()" . 另一种选择是使用@Mapping(expression="java()"

Your mapper will then look like: 然后,您的映射器将如下所示:

@Mapper
public interface MyMapper {

    @Mapping(target = "details.forename", expression = "java(source.list.get(0).details.forename)")
    @Mapping(target = "details.surname", expression = "java(source.list.get(0).details.surname)")
    DestinationObject toDestination(SourceObject source);
}

Note for expression . expression MapStruct will use the text from the expression as is, and won't perform any validation checks. MapStruct将按原样使用表达式中的文本,并且不会执行任何验证检查。 Therefore it can be a bit brittle, as there won't be null and empty checks. 因此,它可以是一个有点脆,因为不会有null和空检查。

我可以用 mapstruct 映射一个从 ArrayList 延伸的 class<object> ?<div id="text_translate"><p> 尝试将从 Arraylist 扩展的 Object 映射到具有列表的 Class 时遇到问题,我的代码是:</p><ol><li> 首先是 Class,它从 ArrayList 延伸:</li></ol><pre> public class ClassOne extends ArrayList&lt;ClassTwo&gt; {}</pre><ol start="2"><li> 我需要映射到:</li></ol><pre> public class ClassTarget { private String companyId; private List&lt;ObjectTarget&gt; fieldListTarget; }</pre><p> 当我声明映射器时,错误是:</p><pre> java: Can't generate mapping method from iterable type to non-iterable type.</pre><p> 我认为错误在extends ArrayList&lt;SomeObject&gt;我不知道如何使用这种类型的 object 映射字段。</p></div></object> - Can I mapping with mapstruct a class that extends from ArrayList<Object>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从ArrayList检索数据 - Retrieving data from ArrayList 我可以用 mapstruct 映射一个从 ArrayList 延伸的 class<object> ?<div id="text_translate"><p> 尝试将从 Arraylist 扩展的 Object 映射到具有列表的 Class 时遇到问题,我的代码是:</p><ol><li> 首先是 Class,它从 ArrayList 延伸:</li></ol><pre> public class ClassOne extends ArrayList&lt;ClassTwo&gt; {}</pre><ol start="2"><li> 我需要映射到:</li></ol><pre> public class ClassTarget { private String companyId; private List&lt;ObjectTarget&gt; fieldListTarget; }</pre><p> 当我声明映射器时,错误是:</p><pre> java: Can't generate mapping method from iterable type to non-iterable type.</pre><p> 我认为错误在extends ArrayList&lt;SomeObject&gt;我不知道如何使用这种类型的 object 映射字段。</p></div></object> - Can I mapping with mapstruct a class that extends from ArrayList<Object>? 从会话变量检索ArrayList - Retrieving a ArrayList from Session variables 从JSON检索结果到ArrayList - Retrieving result from JSON to ArrayList 从数组列表中检索多个值 - Retrieving multiple values from an arraylist 从ArrayList检索URL字符串 - Retrieving Url String From ArrayList 从地图内部检索arraylist - retrieving the arraylist from inside map 检索ArrayList <Object> 从FireBase内部类 - Retrieving ArrayList<Object> from FireBase inner class 通过指定索引从ArrayList检索元素 - Retrieving elemnts from an ArrayList by specifying the indexes 从 hashmap 存储和检索 ArrayList 值 - Storing and Retrieving ArrayList values from hashmap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM