使用Spring Boot从LDAP排序记录

时间:2019-01-29 10:16:52

标签: java spring-boot ldap spring-ldap

如何从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);
}

1 个答案:

答案 0 :(得分:0)

我在这里找到了答案: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