Dozer String to enum mapping

一笑奈何 提交于 2019-12-03 10:19:25

Not sure if this is still an issue, but maybe help for anyone searching. But here is implemented solution to this:

@Override
public Object convert(Object destination, Object source, Class<?> destinationClass,    Class<?> sourceClass) {
    if(source == null)
        return null;
    if(destinationClass != null){
        if(destinationClass.getSimpleName().equalsIgnoreCase("String")){
            return this.getString(source);
        }else if( destinationClass.isEnum()){

            return this.getEnum(destinationClass, source);

        }else{
            throw new MappingException(new StrBuilder("Converter ").append(this.getClass().getSimpleName())
                       .append(" was used incorrectly. Arguments were: ")
                       .append(destinationClass.getClass().getName())
                       .append(" and ")
                       .append(source).toString());
        }
    }
    return null;
}

private Object getString(Object object){
    String value = object.toString();
    return value;
}
private Object getEnum(Class<?> destinationClass, Object source){
    Object enumeration = null;

    Method [] ms = destinationClass.getMethods();
    for(Method m : ms){
        if(m.getName().equalsIgnoreCase("valueOf")){
            try {
                enumeration = m.invoke( destinationClass.getClass(), (String)source);
            }
            catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return enumeration;
        }
    }
    return null;
}

The StrBuilder class when building the exception message is from the apaches common-lang libs. But other than that a simple reflection to solve this issue. Just add to a class that implements CustomConverter and then in your dozer mapping xml file add the following configuration:

<configuration>
    <custom-converters>
        <converter type="com.yourcompany.manager.utils.dozer.converters.EnumStringBiDirectionalDozerConverter">
            <class-a>java.lang.Enum</class-a>
            <class-b>java.lang.String</class-b>
        </converter>
    </custom-converters>
</configuration>

Note that you can only list a configuration once between all of your mapping files (if you have multiple) otherwise dozer will complain. What I typically do is place my custom converter configurations in one file for simplicity. Hope this helps!

There isn't a default enum to String mapping in Dozer. See Data type conversion from Dozer docs. So you have two options:

  • You can write a custom converter that uses generics to handle any enum.
  • Or, you could submit a patch to Dozer to add enum<->String mapping to the default mappings.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!