Orika:从String映射到SomeObjects列表

时间:2017-01-16 10:16:03

标签: java unit-testing converter mapper orika

考虑以下情况:

public class A {
    private String stringA;

    public String getStringA() {
        return stringA;
    }

    public void setStringA(String stringA) {
        this.stringA = stringA;
    }

}

public class B {

    List<SomeObject> someObjects;

    public List<SomeObject> getSomeObjects() {
        if (someObjects == null) {
            someObjects = new ArrayList<SomeObject>();
        }
        return someObjects;
    }

}

public class SomeObject {
    private String stringSomeObject;

    public String getStringSomeObject() {
        return stringSomeObject;
    }

    public void setStringSomeObject(String stringSomeObject) {
        this.stringSomeObject = stringSomeObject;
    }

}

我想从A映射到B。在映射这些内容时,stringA需要映射到stringSomeObject中的SomeObject。我试着写一个Orika-Mapper:

public class MyMapper extends ConfigurableMapper {

    @Override
    protected void configure(MapperFactory factory) {
        ConverterFactory converterFactory = factory.getConverterFactory();
        converterFactory.registerConverter(new StringToSomeObjectConverter());

        factory.classMap(A.class, B.class) //
                .field("stringA", "someObjects") //
                .byDefault() //
                .register();
    }

}

它将类A映射到B,每当遇到从StringList<SomeObject>的转换时,它就会调用自定义转换器:

public class StringToSomeObjectConverter extends CustomConverter<String, List<SomeObject>> {

    private static final String BORROWER_PARTY_TYP_CODE = "147";

    @Override
    public List<SomeObject> convert(String source, Type<? extends List<SomeObject>> destinationType) {
        SomeObject someObject = new SomeObject();
        someObject.setStringSomeObject(source);
        return Arrays.asList(someObject);
    }

}

我写了一个单元测试来确保它有效:

@Test
public void testMap() throws Exception {
    A a = new A();
    a.setStringA("a");

    B outcome = new MyMapper().map(a, B.class);

    assertThat(outcome.getSomeObjects.size(), is(1));
}

可悲的是,这个测试失败了:

java.lang.AssertionError: 
Expected: is <1>
   but: was <0>

似乎转换器从未执行过,所以我尝试调试它。事实上:调试器永远不会到达转换器。难道我做错了什么?这好像是。我知道有更多方法可以使用:mapAToB例如......

好的,我发现了一个解决方案......不!这不是一个解决方案,它只是一种解决方法。我还将stringA定义为List<String>,并定义了一个扩展CustomConverter<String, LoanContrReqERPCrteReqLoanContrBrrwrPty>的转换器。

因为这感觉有点“hacky”,我仍然对一个很好的解决方案感兴趣。 (虽然我只是认为这个解决方案可能没问题:现在两个对象的数据结构比以前更加平等。问题是,对象B来自外部服务,我无法修改它。 )

1 个答案:

答案 0 :(得分:2)

您的映射不起作用,因为您没有someObjects的setter。

当Orika尝试为mapper生成代码时,会检查fieldMapsclassMap的所有sourceProperty是否可读,destinationProperty是否可分配。如果通过此检查,则生成器将字段转换放入生成的映射器中。如果检查失败,Orika只是跳过此字段转换。

您可以使用很少的选项来解决问题:

  • 您可以在课程someObjects中为B字段添加setter:

    public static class B {
    
        List<SomeObject> someObjects;
    
        public List<SomeObject> getSomeObjects() {
            if (someObjects == null) {
                someObjects = new ArrayList<SomeObject>();
            }
            return someObjects;
        }
    
        public void setSomeObjects(List<SomeObject> someObjects) {
            this.someObjects = someObjects;
        }
    }
    
  • 使用自定义映射器而不是转换器:

        factory.classMap(A.class, B.class)
                .customize(
                        new CustomMapper<A, B>() {
                            @Override
                            public void mapAtoB(A a, B b, MappingContext context) {
                                SomeObject someObject = new SomeObject();
                                someObject.setStringSomeObject(a.getStringA());
                                b.getSomeObjects().add(someObject);
                            }
                        }
                )
                .byDefault()
                .register(); 
    

Orika将在解析字段地图后调用customMapper 生成的映射器将如下所示:

    b.setOtherField(a.getOtherField());
    if (customMapper != null) {
        customMapper.map(source, destination); <-- Your mapper invocation
    }
  • 对字段使用以下语法:

        factory.classMap(A.class, B.class)
                .field("stringA", "someObjects[0].stringSomeObject")
                .byDefault()
                .register();
    

生成的映射器将如下所示:

    if (source.getStringA() != null) {
        if (((((java.util.List) destination.getSomeObjects()).size() <= 0 || ((List) destination.getSomeObjects()).get(0) == null))) {
            ((java.util.List) destination.getSomeObjects()).add(0, ((BoundMapperFacade) usedMapperFacades[0]).newObject(((String) source.getStringA()), mappingContext));
        }
    }

    if (!(((java.lang.String) source.getStringA()) == null)) {
        (((java.util.List) destination.getSomeObjects()).get(0)).setStringSomeObject(source.getStringA());
    } else if (!(((java.util.List) destination.getSomeObjects()) == null) && !((((java.util.List) destination.getSomeObjects()).size() <= 0 || ((List) destination.getSomeObjects()).get(0) == null))) {
        ( ((java.util.List) destination.getSomeObjects()).get(0)).setStringSomeObject(null);
    }

此外,Orika中存在使用语法.field("stringA", "elements{stringB}")Incorrect mapper code generated for mapping from a single property to property of collection element)从单个属性映射到集合属性的错误。 Bug于2016年12月31日截止:Fix for bug