java - ModelMapper:确保方法具有零参数并且不返回 void

标签 java spring modelmapper

我对模型映射器进行了以下配置,以将 User 类的实例转换为 ExtendedGetUserDto 的实例。

    public ExtendedGetUserDto convertToExtendedDto(User user) {
        PropertyMap<User, ExtendedGetUserDto> userMap = new PropertyMap<User, ExtendedGetUserDto>() {
            protected void configure() {
                map().setDescription(source.getDescription());
                map().setId(source.getId());
//              map().setReceivedExpenses(
//                      source.getReceivedExpenses()
//                              .stream()
//                              .map(expense -> expenseDtoConverter.convertToDto(expense))
//                              .collect(Collectors.toSet())
//                      );
                Set<GetInvitationDto> result = new HashSet<GetInvitationDto>();
                for (Invitation inv: source.getReceivedInvitations()) {
                    System.out.println("HELLO");
                    //result.add(null);
                }
                //map().setReceivedInvitations(result);
            }
        };
        modelMapper.addMappings(userMap);
        return modelMapper.map(user, ExtendedGetUserDto.class);
    }

在注释掉 setReceivedExpense 之前我收到了这个错误:

org.modelmapper.ConfigurationException: ModelMapper configuration errors:

1) Invalid source method java.util.stream.Stream.map(). Ensure that method has zero parameters and does not return void.

2) Invalid source method java.util.stream.Stream.collect(). Ensure that method has zero parameters and does not return void.

2 errors

在花了一些时间并没有找到根本原因后,我尝试删除 DTO 中所有可疑的循环依赖(我在 GetExpenseDto 中引用了 GetUserDto,返回结果expenseDtoConverter) 我仍然收到同样的错误,我注释掉了 map().setReceivedExpenses(如您在代码中所见)并将其替换为简单的 for 循环。

我收到以下错误:

1) Invalid source method java.io.PrintStream.println(). Ensure that method has zero parameters and does not return void.

为什么我会收到这些错误?

编辑 1

用户.java

@Entity
@Table(name="User")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private long id;

    @Column(name = "name")
    private String name;

    @Size(min=15, max=15)
    @Column(name="image_id")
    private String imageId;

    @Size(max=100)
    @Column(name="description")
    private String description;

    @OneToMany(mappedBy="admin")
    private Set<Group> ownedGroups;

    @ManyToMany(mappedBy="members")
    private Set<Group> memberGroups;

    @OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="owner")
    private Set<Expense> ownedExpenses;

    @ManyToMany(cascade = CascadeType.REFRESH, fetch=FetchType.EAGER)
    private Set<Expense> receivedExpenses;

    @OneToMany(cascade=CascadeType.ALL)
    private Set<Invitation> ownedInvitations;

    @OneToMany(cascade=CascadeType.ALL)
    private Set<Invitation> receivedInvitations;
    //setters and getters for attributes
}

ExtendedGetUserDto.java

public class ExtendedGetUserDto extends GetUserDto {

    private static final long serialVersionUID = 1L;

    private Set<GetInvitationDto> receivedInvitations;
    private Set<GetExpenseDto> receivedExpenses;
    private Set<GetExpenseDto> ownedExpenses;
    private Set<GetGroupDto> ownedGroups;
    private Set<GetGroupDto> memberGroups;
    //setters and getters for attributes
}

最佳答案

您收到这些错误是因为 PropertyMap 限制了您在 configure() 中可以执行的操作。

Javadoc :

PropertyMap uses an Embedded Domain Specific Language (EDSL) to define how source and destination methods and values map to each other. The Mapping EDSL allows you to define mappings using actual code that references the source and destination properties you wish to map. Usage of the EDSL is demonstrated in the examples below.

从技术上讲,它涉及字节码分析、操作和代理,并且它期望 Java 方法调用符合此 EDSL。 这个巧妙的技巧允许 ModelMapper 记录您的映射指令,并随意重播它们

要查看库源代码:您得到的错误是 invalidSourceMethod , 抛出 here in ExplicitMappingVisitor ObjectMapper 使用 ASM library 访问并检测您的 configure 方法的代码.

以下示例是一个独立的可运行示例,应该有助于阐明。我邀请您将它复制到 ModelMapperTest.java 中并实际运行它,然后切换 configure() 中的注释以重现错误:

import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ModelMapperTest {

    public static void main(String[] args) {
        PropertyMap<Foo, FooDTO> propertyMap = new PropertyMap<Foo, FooDTO>() {
            protected void configure() {
                /* This is executed exactly ONCE, to "record" the mapping instructions.
                 * The bytecode of this configure() method is analyzed to produce new mapping code,
                 * a new dynamically-generated class with a method that will basically contain the same instructions
                 * that will be "replayed" each time you actually map an object later.
                 * But this can only work if the instructions are simple enough (ie follow the DSL).
                 * If you add non-compliant code here, it will break before "configure" is invoked.
                 * Non-compliant code is supposedly anything that does not follow the DSL.
                 * In practice, the framework only tracks what happens to "map()" and "source", so
                 * as long as print instructions do not access the source or target data (like below),
                 * the framework will ignore them, and they are safe to leave for debug. */
                System.out.println("Entering configure()");
                // This works
                List<String> things = source.getThings();
                map().setThingsCSVFromList(things);
                // This would fail (not because of Java 8 code, but because of non-DSL code that accesses the data)
                // String csv = things.stream().collect(Collectors.joining(","));
                // map().setThingsCSV(csv);
                System.out.println("Exiting configure()");
            }
        };
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.addMappings(propertyMap);
        for (int i=0; i<5; i++) {
            Foo foo = new Foo();
            foo.setThings(Arrays.asList("a"+i, "b"+i, "c"+i));
            FooDTO dto = new FooDTO();
            modelMapper.map(foo, dto); // The configure method is not re-executed, but the dynamically generated mapper method is.
            System.out.println(dto.getThingsCSV());
        }
    }

    public static class Foo {

        List<String> things;

        public List<String> getThings() {
            return things;
        }

        public void setThings(List<String> things) {
            this.things = things;
        }

    }

    public static class FooDTO {

        String thingsCSV;

        public String getThingsCSV() {
            return thingsCSV;
        }

        public void setThingsCSV(String thingsCSV) {
            this.thingsCSV = thingsCSV;
        }

        public void setThingsCSVFromList(List<String> things) {
            setThingsCSV(things.stream().collect(Collectors.joining(",")));
        }

    }

}

如果你按原样执行它,你会得到:

Entering configure()
Exiting configure()
a0,b0,c0
a1,b1,c1
a2,b2,c2
a3,b3,c3
a4,b4,c4

因此,configure() 只执行一次以记录 映射指令,然后是生成的映射代码(不是configure() 本身) ) 被重播 5 次,每个对象映射一次。

如果您在 configure() 中注释掉带有 map().setThingsCSVFromList(things) 的行,然后取消注释“This would fail”下面的两行,你得到:

Exception in thread "main" org.modelmapper.ConfigurationException: ModelMapper configuration errors:

1) Invalid source method java.util.stream.Stream.collect(). Ensure that method has zero parameters and does not return void.

简而言之,您不能直接在 PropertyMap.configure() 中执行复杂的自定义逻辑,但您可以调用执行此操作的方法。这是因为框架只需要检测处理纯映射逻辑(即 DSL)的字节码部分,它不关心这些方法中发生了什么。

解决方案

(A -- legacy, for Java 6/7)严格按照DSL的要求限制configure的内容。例如,将你的“特殊需求”(日志记录、收集逻辑等)到 DTO 本身的专用方法。

在您的情况下,将逻辑移到别处可能需要更多工作,但想法就在那里。

请注意文档暗示 PropertyMap.configure 并且它的 DSL 主要用于 Java 6/7,但 Java 8 和 lambdas 现在允许优雅的解决方案,其优点是不需要字节码操作魔法。

(B -- Java 8) 查看 other options ,例如 Converter

这是另一个示例(使用与上述相同的数据类,并为整个类型使用 Converter,因为它更适合我的示例,但您可以逐个属性地执行此操作):

    Converter<Foo, FooDTO> converter = context -> {
        FooDTO dto = new FooDTO();
        dto.setThingsCSV(
                context.getSource().getThings().stream()
                        .collect(Collectors.joining(",")));
        return dto;
    };
    ModelMapper modelMapper = new ModelMapper();
    modelMapper.createTypeMap(Foo.class, FooDTO.class)
            .setConverter(converter);
    Foo foo = new Foo();
    foo.setThings(Arrays.asList("a", "b", "c"));
    FooDTO dto = modelMapper.map(foo, FooDTO.class);
    System.out.println(dto.getThingsCSV()); // a,b,c

关于java - ModelMapper:确保方法具有零参数并且不返回 void,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44739738/

相关文章:

Spring 状态机——复用 Submachine

使用构建器模式的 Java 对象映射框架

java - 使用 ModelMapper 转换实体

java - groovy 上的 Cucumber 在查找步骤定义时无法将 null 转换为字符串

java - 关于仅从方法调用返回相同值的问题

java - 如何在 Java 中将 double 位舍入到小数点后两位?

java - ModelMapper 在将实体转换为 DTO 时产生异常

java - 如何使用Java(JSP)将单独选择的日期插入mysql

java - Spring拦截一个类中的异常

javascript - Thymeleaf+spring+Jquery动态添加表单