java - 缺少 Spring LDAP 对象目录映射器注释的属性
问题描述
我正在尝试使用 Spring LDAP 的对象目录映射将对象写入 LDAP 服务器。该对象使用@Entity注释,并且几个字段使用@Attribute注释。
只要填充了所有带注释的字段,一切正常。但是如果字段的值,比如myattribute,是 null 或空字符串,LdapTemplate的create和update方法会抛出错误。服务器拒绝该操作,并抱怨“属性 'myattribute' 的属性值 '' 在语法上不正确”
LDAP 模式允许缺少“myattribute”(它是相关对象类的“可能”属性),但如果存在,则不允许为空(它具有目录字符串语法)。我无法更改架构。
当相应的 POJO 字段为空或为空时,是否有某种方法可以让 Spring LDAP 省略“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 );
}
}
}
推荐阅读
- laravel - 如何获取 Eloquent 模型的当前属性?
- c - Linux中CPU如何切换到中断栈
- c# - 如何从主调用者类中获取孩子的类名
- javascript - 如何将期权日期设置为昨天今天和明天?
- javascript - JS如何从导入器文件夹导入文件,而不是所有者文件夹
- matplotlib - matplotlib.pyplot.plot 函数如何仅使用一个参数?
- java - 返回空的不可变集合而不是分配一个新的空集合有什么缺点?
- javascript - 如何使用 Web 浏览器检查器工具查看网站的 js 文件?
- visual-studio - 无法安装 Blazor 扩展
- ios - 是否可以使用 PDF 远程数据初始化 UIImage?