缺少具有 Spring LDAP 对象目录映射器注释的属性

Missing attributes with Spring LDAP object-directory mapper annotations

我正在尝试使用 Spring LDAP 的对象目录映射将对象写入 LDAP 服务器。对象用 @Entity 注释,几个字段用 @Attribute.

注释

只要所有带注释的字段都被填充,一切正常。但是如果字段的值,比如 myattribute,是 null 或空字符串,createupdate LdapTemplate 的方法抛出错误。服务器拒绝操作,投诉 "Attribute value '' for attribute 'myattribute' is syntactically incorrect"

LDAP 架构允许 'myattribute' 缺失(它是相关对象类的 "may" 属性),但如果它存在,则不允许为空(它有目录字符串语法)。我无法更改架构。

有什么方法可以让 Spring LDAP 在相应的 POJO 字段为 null 或空时省略 'myattribute',而不是尝试创建具有空值的属性?

我找到了一个解决方案,它对我的​​应用程序来说可能不是最优雅的,但它确实有效。不要将 Java 字段声明为 String 类型,而是将其声明为 List 类型。然后,在 setter 中,如果值为空白或空值,我将列表长度设置为零,而不是设置单个空值。

@Entry( objectClasses={"myObject"} )
public class MyDataContainer {

    @Attribute("myattribute")
    private List<String> _myattribute = new ArrayList<String>(1);

    public String getMyAttribute() {
        if ( _myattribute.length() > 0 ) {
            return _myattribute.get(0);
        }
        return null;
    }

    public void setMyAttribute( String value ) {
        _myattribute.clear();
        value = ( value == null ) ? "" : value.trim();
        if ( ! "".equals( value ) ) {
            _myattribute.add( value );
        }
    }
}