提问者:小点点

如何打印javax.naming.directory中的所有键值对。属性?


考虑代码:

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

NamingEnumeration<SearchResult> searchResults = null;
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
constraints.setTimeLimit(GloriaParams.LDAP_TIMEOUT_LIMIT);
constraints.setReturningAttributes(returningAttributes);

try {
  searchResults = HelperClass.getLdapContext().search(userId, constraints);
  Attributes attributes = ((SearchResult) searchResults.next()).getAttributes();
} catch (Exception e){
  e.printStackTrace();
}

我需要在属性中打印所有键值对。

到目前为止,我正在使用

LOG.info(attributes.get(searchKey).get().toString());

但是对于这个需要知道键,但是我不知道所有的键。那么如何打印所有的键值对呢?


共1个答案

匿名用户

NamingEnumeration<String> allAttributeIDs =  attributes.getIDs();
        try {
            while(allAttributeIDs.hasMore()){
                String attributeId = allAttributeIDs.next();
                LOGGER.info(attributeId + ": " + attributes.get(attributeId).get().toString());
            }
        } catch (NamingException e) {
            GloriaExceptionLogger.log(e, UtilServicesBean.class);
        }