简体   繁体   English

推土机-转换器后调用映射器

[英]Dozer - invoke mapper after converter

I've got a dozer mapping that returns a User through a custom converter by the parameter. 我有一个推土机映射,该映射通过参数通过自定义转换器返回用户。 This works as it should, but now this User should be converted to a Official. 这可以正常工作,但是现在应该将此用户转换为正式用户。

I tried to do this by letting the converter return the User and making another dozer xml file to convert the User to the Official. 我试图通过让转换器返回用户并制作另一个推土机xml文件将用户转换为官方文件来做到这一点。 I added this xml file to the dozerfactory, but it doesn't seem to be invoked. 我将此XML文件添加到了dozerfactory,但似乎没有被调用。

I'm getting: "Exepected Official, actual: User". 我得到:“预期官方,实际:用户”。 How could I let the userToOfficial.xml file be invoked? 如何让userToOfficial.xml文件被调用?

First dozer file: 第一个推土机文件:

<mapping>
<class-a>be.someClass</class-a>
<class-b>be.anotherClassWithOfficial</class-b>


<field custom-converter-id="OfficialConverter">
  <a>someString</a>
  <b>official</b>
</field>

tried adding, but doesn't work: 尝试添加,但不起作用:

<b-hint>be.Official</b-hint>

the second dozer file (should be invoked after the converter but isn't): 第二个推土机文件(应在转换器之后调用,但不能):

<mapping>
<class-a>be.User</class-a>
<class-b>be.Official</class-b>
<field>
  <a>mail</a>
  <b>email</b>
</field>

The converter: 转换器:

public class OfficialConverter implements CustomConverter {

/** The ldap local. */
private UserLocal userLocal;

@Override
public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue,
        Class < ? > destinationClass, Class < ? > sourceClass) {
    if (sourceFieldValue == null) {
        return null;
    }
    if (sourceFieldValue instanceof String) {
        User user= userLocal
                .getUserByLogin((String) sourceFieldValue);
        return user;

    }
    return null;
}

public UserLocal getUserLocal() {
    return userLocal;
}


public void setUserLocal(UserLocal userLocal) {
    this.userLocal= userLocal;
}

} }

As per your customconverter, it seems you are mapping from String to User and now instead of User , you want an object of Offical . 根据您的customconverter,似乎您是从String映射到User ,现在想要的是Offical而不是User Right? 对? and you want to automate this User to Offical conversion using dozer. 并且要自动执行此UserOffical使用推土机转换。 Right? 对? If yes, then the you need to invoke the new dozer in the custom converter itself using mapper.map() method. 如果是,那么您需要使用mapper.map()方法在自定义转换器中调用新的推土机。 Your current use-case will not invoke it automatically. 您当前的用例不会自动调用它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM