gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:05:30 25 4
gpt4 key购买 nike

我想使用 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

有人知道我做错了什么吗?

最佳答案

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

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 [主要] 调试 org.dozer.MappingProcessor - 映射:DozerTest$OldPackage.version --> DozerTest$NewPackage.version 值:1.1 --> be.smals.safe.centralplatform.core.utils.DozerTest$Version@1490eb5 MAPID:1.1

关于java - Dozer 5.4 setCustomConverter 在同名但不同类型的字段之间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21454807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com