1

我得到这个例外:

Field mapping error -->
MapId: null
Type: null
Source parent class: com.ParentObject
Source field name: field1
Source field type: class com.OnOff1BitEnum
Source field value: OFF
Dest parent class: com.SubParent
Dest field name: subParent.field2
Dest field type: java.lang.Boolean java.lang.NullPointerException
at org.dozer.util.ReflectionUtils.invoke(ReflectionUtils.java:270)
at org.dozer.propertydescriptor.GetterSetterPropertyDescriptor.getDeepSrcFieldValue(GetterSetterPropertyDescriptor.java:116)
at org.dozer.propertydescriptor.GetterSetterPropertyDescriptor.getPropertyValue(GetterSetterPropertyDescriptor.java:69)
at org.dozer.fieldmap.FieldMap.getDestValue(FieldMap.java:141)

这是映射字段:

<mapping>
    <class-a>com.ParentObject</class-a>
    <class-b>com.SubParent</class-b>

    <field custom-converter="com.OnOffEnumToBooleanConverter">
        <a>field1</a> 
        <b get-method="isField2">subParent.field2</b> 
    </field>
</mapping>

我使用的推土机版本是 5.4.0

更新:在这篇文章中添加了转换器代码。当我在这里设置一个断点时,这没有达到

public class OnOffEnumToBooleanConverter implements org.dozer.CustomConverter {
/**
 * {@inheritDoc}
 */
@Override
public Object convert(Object dest, Object src, Class<?> destClass, Class<?> srcClass) {
    if (src == null) {
        return null;
    }
            if (src instanceof com.OnOff1BitEnum) {
        boolean canonicalObject;
        if ((com.OnOff1BitEnum) src == com.OnOff1BitEnum.ON) {
            canonicalObject = true;
        } else if ((com.OnOff1BitEnum) src == com.OnOff1BitEnum.OFF) {
            canonicalObject = false;
        } else { // Unreachable Code To Test
            throw new MappingException("Converter " + this.getClass().getCanonicalName() + " used incorrectly.  Arguments passed in were: "
                    + dest + " and " + src);
        }
        return canonicalObject;
    } 
}
4

1 回答 1

1

也许它发生是因为您缺少“设置配置”,请尝试明确指定它并查看是否可以解决它或发生不同的错误,例如

<field custom-converter="com.OnOffEnumToBooleanConverter">
        <a>field1</a> 
        <b get-method="isField2" set-method="field2">subParent.field2</b> 
</field>

更新:

我必须正确格式化您的 java 代码才能读取...这是布尔值的“if and else”问题...没有第三个条件...仔细看一下:

if (src instanceof com.OnOff1BitEnum)
{
  boolean canonicalObject;
  if ((com.OnOff1BitEnum) src == com.OnOff1BitEnum.ON)
  {
    canonicalObject = true;
  }
  else
  {
    if ((com.OnOff1BitEnum) src == com.OnOff1BitEnum.OFF)
    {
      canonicalObject = false;
    }
    else
    { // Unreachable Code To Test
      throw new MappingException("Converter " + this.getClass().getCanonicalName()
          + " used incorrectly.  Arguments passed in were: " + dest + " and " + src);
    }
  }
  return canonicalObject;
}

这就是原始代码中没有括号的“if else 构造”的弊端。您的“convert”方法也返回 Object...所以您的“ boolean canonicalObject”必须是“ Boolean canonicalObject”而不是原始类型。

应该:

@Override
public Object convert(Object dest, Object src, Class<?> destClass, Class<?> srcClass) 
{    
    if (src instanceof com.OnOff1BitEnum)
    {
      if ((com.OnOff1BitEnum) src == com.OnOff1BitEnum.ON)
      {
        return Boolean.TRUE;
      }
      else
      {
        return Boolean.FALSE;
      }
    }
    return null;
}
于 2013-05-16T18:03:16.570 回答