1

我有一组 JAX-WS 生成的类,它们在另一个包中重复。现在我想使用 Dozer 在这些类之间进行映射。我现在遇到的问题是基于 JAX-WS 没有为 Collection 类型生成设置器。set(List<Foo> foo)我不得不打电话而不是打电话getFoo().addAll(List<Foo>)。我如何配置 Dozer(通过 XML 和 API)来尊重这一点?

4

1 回答 1

2

好的,我想出了如何使用静态推土机 xml 配置来做到这一点:

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
      http://dozer.sourceforge.net/schema/beanmapping.xsd">
<mapping>
    <class-a>foo.ArrayOfItem
    </class-a>
    <class-b>bar.ArrayOfItem</class-b>
    <field>
        <a>item</a>
        <b set-method="getItem().addAll">item</b>
        <b-hint>bar.Item</b-hint>
    </field>
</mapping>
<mapping>
    <class-a>foo.Item</class-a>
    <class-b>bar.Item</class-b>
</mapping>

我错过了所需的<b-hint>bar.Item</b-hint>. 现在我只需要在运行时通过 Dozer API 执行此操作...

这失败了,java.lang.ClassCastException: foo.Item cannot be cast to bar.Item因为没有应用提示:

    BeanMappingBuilder mappingBuilder = new BeanMappingBuilder() {

        @Override
        protected void configure() {
            mapping(
                    "foo.ArrayOfItem",
                    "bar.ArrayOfItem").fields("item", "item",
                    new FieldsMappingOption() {
                        public void apply(
                                FieldMappingBuilder fieldMappingBuilder) {
                            FieldDefinitionBuilder itemFieldDefinitionBuilder = fieldMappingBuilder
                                    .b("item");
                            itemFieldDefinitionBuilder
                                    .theSetMethod("getItem().addAll");
                        }
                    }).hintB(bar.Item.class);
        }
    };

这是它的工作原理:

BeanMappingBuilder mappingBuilder = new BeanMappingBuilder() {

        @Override
        protected void configure() {
            mapping(
                    "foo.ArrayOfItem",
                    "bar.ArrayOfItem").fields("item", "item",
                    new FieldsMappingOption() {
                        public void apply(
                                FieldMappingBuilder fieldMappingBuilder) {
                            FieldDefinitionBuilder itemFieldDefinitionBuilder = fieldMappingBuilder
                                    .b("item");
                            itemFieldDefinitionBuilder
                                    .theSetMethod("getItem().addAll");
                        }
                    }, FieldsMappingOptions.hintB(bar.Item.class));
        }
    };
于 2010-11-12T10:57:24.750 回答