Java 从NamingEnumeration中提取数据
我的应用程序在LDAP服务器上搜索人员Java 从NamingEnumeration中提取数据,java,list,ldap,Java,List,Ldap,我的应用程序在LDAP服务器上搜索人员 return ldapTemplate.search("", "(objectclass=person)", new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { return
return ldapTemplate.search("", "(objectclass=person)", new AttributesMapper() {
public Object mapFromAttributes(Attributes attrs)
throws NamingException {
return attrs.get("cn").getAll();
}
});
它返回namingumeration
对象的列表,其中包含向量。每个向量可以包含一个或多个值。
我可以用这个代码打印人名
for(NamingEnumeration ne : list){
while (ne.hasMore()) {
System.out.println("name is : " + ne.next().toString());
}
}
因为我的ldap搜索可以包含多个值,所以在
namingumeration
对象中以向量形式出现。如何从中获取多个值。同时使用Java5引入的for
语法迭代列表
您不应该调用hasMore()
如果列表不支持迭代器接口,则需要使用旧表单:
for ( Enumeration e = v.elements() ; e.hasMoreElements() ; ) {
String a = (String) e.nextElement();
System.out.println( a );
}
当您使用
javax.naming.namingumeration
的java.util.List
时
List<NamingEnumeration<Vector>> list
请注意,许多人认为这是过时的,尽管不是不推荐的。此外,封闭的集合可以使用类型参数。如果你有选择的话,考虑一下其中的一种选择:
List<NamingEnumeration<Vector<T>>>
List<NamingEnumeration<List<T>>>
列表
列表
此代码片段将用于未知属性名称和单个或多个值(例如多个对象类):
使用SpringLDAP 2.3.1和AttributesMapper实现:
<!-- https://mvnrepository.com/artifact/org.springframework.ldap/spring-ldap-core -->
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
org.springframework.ldap
SpringLDAP核心
2.3.1.1发布
在此示例代码中,searchResultList包含所有条目,每个条目都表示为属性映射(具有一个或多个值):
List searchResultList=sourceLdapTemplate.search(searchBase,filter.encode(),SearchControls.ONELEVEL\u作用域,新属性映射(){
@凌驾
公共映射mapFromAttributes(属性属性)引发NamingException{
Map attrsMap=new HashMap();
NamingEnumeration attrIdEnum=attributes.getId();
while(attrIdEnum.hasMoreElements()){
//获取属性id:
字符串attrId=attrIdEnum.next();
//获取所有属性值:
Attribute attr=attributes.get(attrId);
NamingEnumeration attrValuesEnum=attr.getAll();
while(attrValuesEnum.hasMore()){
如果(!attrsMap.containsKey(attrId))
attrsMap.put(attrId,newarraylist());
attrsMap.get(attrId).add(attrValuesEnum.next().toString());
}
}
返回属性映射;
}
});
现在,使用searchResultList如下所示:
for (Map<String, List<String>> attrsMap : searchResultList) {
for (String objectClass : m.get("objectClass")) {
// do something with this objectClass...
}
}
for(映射属性映射:searchResultList){
for(字符串objectClass:m.get(“objectClass”)){
//使用此objectClass执行某些操作。。。
}
}
我是这么问的;“如何从中获取多个值。”。这意味着我如何知道向量包含一个或多个元素。使用counter?@imrantariq:您正在检查IIUC的java.util.List
,您可以结合@stacker的有用建议,如图所示,
<!-- https://mvnrepository.com/artifact/org.springframework.ldap/spring-ldap-core -->
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
List<Map<String, List<String>>> searchResultList = sourceLdapTemplate.search(searchBase, filter.encode(), SearchControls.ONELEVEL_SCOPE, new AttributesMapper<Map<String, List<String>>>() {
@Override
public Map<String, List<String>> mapFromAttributes(Attributes attributes) throws NamingException {
Map<String, List<String>> attrsMap = new HashMap<>();
NamingEnumeration<String> attrIdEnum = attributes.getIDs();
while (attrIdEnum.hasMoreElements()) {
// Get attribute id:
String attrId = attrIdEnum.next();
// Get all attribute values:
Attribute attr = attributes.get(attrId);
NamingEnumeration<?> attrValuesEnum = attr.getAll();
while (attrValuesEnum.hasMore()) {
if (!attrsMap.containsKey(attrId))
attrsMap.put(attrId, new ArrayList<String>());
attrsMap.get(attrId).add(attrValuesEnum.next().toString());
}
}
return attrsMap;
}
});
for (Map<String, List<String>> attrsMap : searchResultList) {
for (String objectClass : m.get("objectClass")) {
// do something with this objectClass...
}
}