Dozer 和 JAX-WS 生成的类的映射问题

发布于 2024-10-02 00:05:19 字数 235 浏览 2 评论 0原文

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

I have a set of JAX-WS generated classes which are duplicated in another package. Now I want to map between those classes by using Dozer. The problem I have now is based on JAX-WS not generating setters for Collection types. Instead of calling set(List<Foo> foo) I have to call getFoo().addAll(List<Foo>).
How can I configure Dozer(via XML and API) to respect this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缺⑴份安定 2024-10-09 00:05:19

好吧,我想出了如何使用静态 dozer 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>

我缺少所需的 bar.Item
现在我只需要在运行时通过 Dozer API 执行此操作...

这将失败,并显示 java.lang.ClassCastException: foo.Item can 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));
        }
    };

Ok, I figured out how to do it with a static dozer xml configuration:

<?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>

I was missing the the required <b-hint>bar.Item</b-hint>.
Now I only need to do this at runtime via Dozer API...

This fails with java.lang.ClassCastException: foo.Item cannot be cast to bar.Item as the hint somehow isn't applied:

    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);
        }
    };

This is how it works:

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));
        }
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文