Dozer 5.4 setCustomConverter在同名但不同类型的字段之间不起作用

时间:2014-01-30 11:10:59

标签: java dozer

我希望在源和目标对象中具有相同名称但不同类型的字段之间使用Dozer进行自定义类型转换。 我准备了解释我的意图的代码片段:

package com;

import org.dozer.CustomConverter;
import org.dozer.DozerBeanMapper;
import org.dozer.DozerConverter;

import java.util.Arrays;

public class DozerTest
{
    public static void main(String[] args)
    {
        DozerBeanMapper mapper = new DozerBeanMapper();

        mapper.setCustomConverters(Arrays.<CustomConverter>asList(new DozerConverter<Version, String>(Version.class, String.class)
                {
                    @Override public String convertTo(Version version, String versionOrig)
                    {
                        return version.getFullVersion();
                    }

                    @Override public Version convertFrom(String version, Version versionOrig)
                    {
                        return new Version(version);
                    }
                }));

        OldPackage oldPackage = new OldPackage();

        oldPackage.setVersion("1.1");

        NewPackage newPackage = mapper.map(oldPackage, NewPackage.class);
        System.out.println(newPackage.getVersion().getFullVersion());
    }

    public static class NewPackage
    {
        private Version version;

        public Version getVersion()
        {
            return version;
        }

        public void setVersion(Version version)
        {
            this.version = version;
        }
    }

    public static class OldPackage
    {
        private String version;

        public String getVersion()
        {
            return version;
        }

        public void setVersion(String version)
        {
            this.version = version;
        }
    }

    private static class Version
    {
        private final String fullVersion;

        private Version(String fullVersion)
        {
            this.fullVersion = fullVersion;
        }

        public String getFullVersion()
        {
            return fullVersion;
        }
    }
}

我收到此错误消息:

Exception in thread "main" org.dozer.MappingException: Illegal object type for the method 'setVersion'. 
 Expected types: 
com.DozerTest$Version
 Actual types: 
java.lang.String

任何人都知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

我认为您忘记将映射添加到映射器:

BeanMappingBuilder foo = new BeanMappingBuilder() {

        @Override
        protected void configure() {
            mapping(OldPackage.class, NewPackage.class).fields("version", "version", FieldsMappingOptions.customConverter(ConverterImpl.class));
        }
    };
mapper.addMapping(foo);

我创建了一个静态内部类来表示你的DozerConverter实现,以便在你的BeanMappingBuilder中引用它:

static class ConverterImpl extends DozerConverter<Version, String> {

    ConverterImpl() {
        super(Version.class, String.class);
    }

    @Override public String convertTo(Version version, String versionOrig)
    {
        return version.getFullVersion();
    }

    @Override public Version convertFrom(String version, Version versionOrig)
    {
        return new Version(version);
    }
}

全班:

import org.dozer.CustomConverter;
import org.dozer.DozerBeanMapper;
import org.dozer.DozerConverter;
import org.dozer.loader.api.BeanMappingBuilder;
import org.dozer.loader.api.FieldsMappingOptions;
import java.util.Collections;

public class DozerTest
{
   public static void main(String[] args)
   {
    DozerBeanMapper mapper = new DozerBeanMapper();

    mapper.setCustomConverters(Collections.<CustomConverter>singletonList(new ConverterImpl()));

    BeanMappingBuilder foo = new BeanMappingBuilder() {

        @Override
        protected void configure() {
            mapping(OldPackage.class, NewPackage.class).fields("version", "version", FieldsMappingOptions.customConverter(ConverterImpl.class));
        }
    };
    mapper.addMapping(foo);

    OldPackage oldPackage = new OldPackage();

    oldPackage.setVersion("1.1");

    NewPackage newPackage = mapper.map(oldPackage, NewPackage.class);
    System.out.println(newPackage.getVersion().getFullVersion());
}

static class ConverterImpl extends DozerConverter<Version, String> {

    ConverterImpl() {
        super(Version.class, String.class);
    }

    @Override public String convertTo(Version version, String versionOrig)
    {
        return version.getFullVersion();
    }

    @Override public Version convertFrom(String version, Version versionOrig)
    {
        return new Version(version);
    }
}

public static class NewPackage
{
    private Version version;

    public Version getVersion()
    {
        return version;
    }

    public void setVersion(Version version)
    {
        this.version = version;
    }
}

public static class OldPackage
{
    private String version;

    public String getVersion()
    {
        return version;
    }

    public void setVersion(String version)
    {
        this.version = version;
    }
}

private static class Version
{
    private final String fullVersion;

    private Version(String fullVersion)
    {
        this.fullVersion = fullVersion;
    }

    public String getFullVersion()
    {
        return fullVersion;
    }
}

}

执行此测试类时,您将获得所需的输出:

13:56:34.994 [main] DEBUG org.dozer.MappingProcessor - MAPPED:DozerTest $ OldPackage.version - &gt; DozerTest $ NewPackage.version VALUES:1.1 - &gt; be.smals.safe.centralplatform.core.utils.DozerTest$Version@1490eb5 MAPID: 1.1