gpt4 book ai didi

java - Spring LDAP 对象目录映射器注释缺少属性

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:29 25 4
gpt4 key购买 nike

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

只要填充了所有带注释的字段,一切就正常了。但是,如果字段的值(例如 myattribute)为 null 或空字符串,则 LdapTemplatecreateupdate 方法会抛出错误。服务器拒绝该操作,并提示“属性‘myattribute’的属性值‘’在语法上不正确”

LDAP 模式允许缺少“myattribute”(它是相关对象类的“可能”属性),但如果存在,则不允许为空(它具有目录字符串语法)。我无法更改架构。

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

最佳答案

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

@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 );
}
}
}

关于java - Spring LDAP 对象目录映射器注释缺少属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52417181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com