使用 Spring 引导从 LDAP 排序记录

Sorted records from LDAP with Spring Boot

如何正确地从 LDAP 目录中获取排序的记录。由于数据量大,全部加载到内存然后排序非常耗时。 这就是为什么我想要(使用 Spring Boot/SpringLDAP)从 LDAP 中获取已经排序的记录。

我尝试在@Repository 中创建

@Repository
public interface XxxRepository extends LdapRepository<Xxx>, Serializable {

   List<Xxx> findAllByOrderByNameAsc(LdapQuery ldapQuery);

}

无效,NullPointerException。

我的@Entity 看起来像:

@Entry(base = "", objectClasses = {"xxx", "xxx"})
public class Xxx implements Serializable {
   @Id
   private Name dn;

   @Attribute(name = "name")
   @DnAttribute(value = "name", index = 0)
   private String name;

   .
   .
   .
}

我的@Service

public void search() {
   String filterConditions = "(&(objectClass=Xxx)";
   filterConditions += "(name=*)";
   LdapQuery query = query().base(BASE_UNIT).filter(filterConditions + ")");
   List<Xxx> xxx= xxxRepository.findAllByOrderByNameAsc(query);
}

我在这里找到了答案:Spring-LDAP LdapTemplateSortedSearchITest

public void testSearch_SortControl_ConvenienceMethod() {
    SortControlDirContextProcessor requestControl;

    // Prepare for first search
    requestControl = new SortControlDirContextProcessor("cn");
    tested.search(BASE, FILTER_STRING, searchControls, callbackHandler,
            requestControl);
    int resultCode = requestControl.getResultCode();
    boolean sorted = requestControl.isSorted();
    assertThat("Search result should have been sorted: " + resultCode, sorted).isTrue();
    List list = callbackHandler.getList();
    assertSortedList(list);
}

它使用SortControlDirContextProcessor