我正在使用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;
    }
}

09-16 00:40