gpt4 book ai didi

java - DozerConverter 提示其中一个类中的 equals 方法

转载 作者:行者123 更新时间:2023-12-02 03:25:12 24 4
gpt4 key购买 nike

我正在使用 DozerConverter 将 String 转换为我的一个类。

代码如下:

class A {
String property;
}

class B {
MyType property;
}

class MyType {
// things...

@Override
public boolean equals(Object obj) {

return getFlatValue().equals(((MyType)obj).getFlatValue());

}
}

public class MyTypeConverter extends DozerConverter<String, MyType> {

public MyTypeConverter() {
super(String.class, MyType.class);
}

@Override
public String convertFrom(MyType source, String destination) {
destination = source.getFlatValue();
return destination;
}

@Override
public MyType convertTo(String source, MyType destination) {
destination = new MyType();
destination.parseFromString(source);
return destination;
}

}

public enum E_BeanMapper {

INSTANCE;

public Mapper getMapper() {
DozerBeanMapper mapper = new DozerBeanMapper();
BeanMappingBuilder beanMappingBuilder = new BeanMappingBuilder() {

@Override
protected void configure() {
mapping(A.class, B.class).fields("property", "property", FieldsMappingOptions.customConverter(MyTypeConverter.class));

}
};
mapper.addMapping(beanMappingBuilder);
return mapper;
}

}

但是当我尝试从一个对象转换为另一个对象时,出现以下异常:

java.lang.ClassCastException: java.lang.String cannot be cast to com.mycompany.myproject.types.MyType
at com.mycompany.myproject.types.MyType.equals(MyType.java:95)
at org.dozer.converters.PrimitiveOrWrapperConverter.convert(PrimitiveOrWrapperConverter.java:63)
at org.dozer.MappingProcessor.mapOrRecurseObject(MappingProcessor.java:449)
at org.dozer.MappingProcessor.mapFromFieldMap(MappingProcessor.java:361)
at org.dozer.MappingProcessor.mapField(MappingProcessor.java:307)
at org.dozer.MappingProcessor.map(MappingProcessor.java:267)
at org.dozer.MappingProcessor.mapToDestObject(MappingProcessor.java:216)
at org.dozer.MappingProcessor.createByCreationDirectiveAndMap(MappingProcessor.java:196)
at org.dozer.MappingProcessor.mapCustomObject(MappingProcessor.java:512)
at org.dozer.MappingProcessor.mapOrRecurseObject(MappingProcessor.java:465)
at org.dozer.MappingProcessor.mapFromFieldMap(MappingProcessor.java:361)
at org.dozer.MappingProcessor.mapField(MappingProcessor.java:307)
at org.dozer.MappingProcessor.map(MappingProcessor.java:267)
at org.dozer.MappingProcessor.mapToDestObject(MappingProcessor.java:216)
at org.dozer.MappingProcessor.createByCreationDirectiveAndMap(MappingProcessor.java:196)
at org.dozer.MappingProcessor.mapGeneral(MappingProcessor.java:170)
at org.dozer.MappingProcessor.map(MappingProcessor.java:104)
at org.dozer.MappingProcessor.map(MappingProcessor.java:99)
at org.dozer.DozerBeanMapper.map(DozerBeanMapper.java:120)
at com.mycompany.myproject.mypackage.AnotherClass.oneMethod(AnotherClass.java:60)

我做错了什么?

最佳答案

至少,您的 equals 方法不能保证始终传入 MyType 实例,因此您需要防范这种情况。

@Override
public boolean equals(Object obj) {
if(obj instanceof MyType) {
return getFlatValue().equals(((MyType)obj).getFlatValue());
} else {
return false;
}
}

关于java - DozerConverter 提示其中一个类中的 equals 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39041749/

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