如何打印javax.naming.directory.Attributes中的所有键值对?[副本]

juzqafwq  于 2023-05-21  发布在  Java
关注(0)|答案(1)|浏览(120)

此问题已在此处有答案

Extract data from NamingEnumeration(3个答案)
4天前关闭。
考虑代码:

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());

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

vhmi4jdf

vhmi4jdf1#

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

相关问题