How to tell castor to marshall a null field to an empty tag?

◇◆丶佛笑我妖孽 提交于 2019-12-06 06:45:11

One way to do this is with a GeneralizedFieldHandler. It's a bit of a hack but it will work for other fields that are Strings.

Example:

<mapping>
    <class name="Entity">
        <field name="id" type="integer"/>
        <field name="name" type="string"/>
        <field name="description" type="string" handler="NullHandler"/>
    </class>
</mapping>


public class NullHandler extends GeneralizedFieldHandler {

    @Override
    public Object convertUponGet( Object arg0 )
    {
        if( arg0 == null )
        {
            return "";
        }

        return arg0;
    }

    @Override
    public Object convertUponSet( Object arg0 )
    {
        return arg0;
    }

    @Override
    public Class getFieldType()
    {
        return String.class;
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!