我有一个Rest-Application,它有一个方法可以从LDAP中获取所有用户。 spring方法看起来像:
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "users", method = RequestMethod.GET, consumes = "application/json", produces = "application/json")
public List<User> getUsers(@RequestParam(
@RequestParam(required = false, value = "start") Long start,
@RequestParam(required = false, value = "sort") String sort,
@RequestParam(required = false, value = "asc") boolean asc)
throws Exception {
LdapContext ctx= .....
List<User> Users;
try {
Users = searchUsers(ctx, "ou=users", start, sort, asc);
} catch (Exception e) {
....
} finally {
ctx.close();
}
return Users;
}
搜索所有用户的方法是:
private List<User> searchUsers(LdapContext ctx, String ou, Long start,
String sort, boolean asc) throws Exception {
NamingEnumeration<SearchResult> enumResult = null;
List<User> LdapUserList = null;
int totalSize = 0;
try {
SearchControls searchCtrls = new SearchControls();
searchCtrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchCtrls.setReturningAttributes(attributes);
// Create a sort control that sorts based on sort-field
String sortKey = sort;
ctx.setRequestControls(new Control[] { new SortControl(sortKey, false) });
String filter = "(&(objectClass=inetOrgPerson))";
enumResult = ctx.search(ou, filter, searchCtrls);
// Iterate over sorted search results
while (enumResult != null && enumResult.hasMore()) {
totalSize++;
SearchResult result = (SearchResult) enumResult.next();
System.out.println("Result: "+result.getName());
User ldapUser = new User();
ldapUser.add(......)
// some code to fullfil the ldapUser with attributes
UserList.addElement(ldapUser);
}
} catch (Exception e) {
....
}
System.out.println(totalSize);
return UserList;
}
我试图通过链接“/ users?sort = uid”来调用我的spring应用程序。
问题是,我从我的ou获得所有用户,但他们没有排序。 如果我打印出变量“sort”,我得到正确的“uid”。所以@RequestParam也在工作。 如何在服务器端对ldap响应的结果进行排序?错误在哪里?
最后,我想在代码“asc”和“start”中添加更多param,用于排序顺序和start-index。
感谢您的帮助!