I am trying to fetch the data from a LDAP server. Till now, I have been successfully able to connect to the server and get a list of all the records. But when I try to GET a single record, I get the following error
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [javax.naming.Name]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.data.repository.support.ReflectionRepositoryInvoker.convertId(ReflectionRepositoryInvoker.java:289) ~[spring-data-commons-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.repository.support.CrudRepositoryInvoker.invokeFindById(CrudRepositoryInvoker.java:92) ~[spring-data-commons-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory$UnwrappingRepositoryInvoker.lambda$invokeFindById$2(UnwrappingRepositoryInvokerFactory.java:95) ~[spring-data-rest-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) ~[na:na]
...
I have my LDAP config setup as such
@Configuration
@EnableLdapRepositories(basePackages = "com.sayak.repository.ldap")
public class LdapConfig {
@Value("${ldap.urls}")
private String ldapUrls;
@Value("${ldap.base.dn}")
private String ldapBaseDn;
@Value("${ldap.username}")
private String ldapSecurityPrincipal;
@Value("${ldap.password}")
private String ldapPrincipalPassword;
@Bean
ContextSource contextSource() {
LdapContextSource ldapContextSource = new LdapContextSource();
ldapContextSource.setUrl(ldapUrls);
ldapContextSource.setBase(ldapBaseDn);
ldapContextSource.setUserDn(ldapSecurityPrincipal);
ldapContextSource.setPassword(ldapPrincipalPassword);
return ldapContextSource;
}
@Bean
LdapTemplate ldapTemplate(ContextSource contextSource) {
return new LdapTemplate(contextSource);
}
}
The entry for LDAP data is
@Entry(objectClasses = {"inetOrgPerson", "organizationalPerson", "person", "top"})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class LdapUser {
@Id
private Name distinguishedName;
@Attribute(name = "cn")
private String commonName;
@Attribute(name = "sn")
private String surname;
@Attribute(name = "givenName")
private String givenName;
@Attribute(name = "ou")
private String organisationalUnit;
@Attribute(name = "uid")
private String userId;
@Attribute(name = "mail")
private String email;
public LdapUser(LdapUser other) {
this.distinguishedName = other.distinguishedName;
this.commonName = other.commonName;
this.surname = other.surname;
this.givenName = other.givenName;
this.organisationalUnit = other.organisationalUnit;
this.userId = other.userId;
this.email = other.email;
}
}
And my repository is setup like
@Repository
public interface LdapUserRepository extends LdapRepository<LdapUser> {
LdapUser findByCommonName(String commonName);
List<LdapUser> findByCommonNameContainingIgnoreCase(String commonName);
}
When I make a GET request at /ldapUsers
, I get a valid response like
{
"_embedded": {
"ldapUsers": [
{
"commonName": "Katherine Ito",
"surname": "Ito",
"givenName": "Katherine",
"organisationalUnit": "Peons",
"userId": "ItoK",
"email": "[email protected]",
"_links": {
"self": {
"href": "http://localhost:6332/api/v1/ldapUsers/cn=Katherine%20Ito,ou=Peons"
},
"ldapUser": {
"href": "http://localhost:6332/api/v1/ldapUsers/cn=Katherine%20Ito,ou=Peons"
}
}
},
...
]
},
"_links": {
"self": {
"href": "http://localhost:6332/api/v1/ldapUsers"
},
"profile": {
"href": "http://localhost:6332/api/v1/profile/ldapUsers"
},
"search": {
"href": "http://localhost:6332/api/v1/ldapUsers/search"
}
}
}
But when I make a GET request to /ldapUsers/cn=Katherine%20Ito,ou=Peons
(as evident by the self.href
in the first response, it throws the error.
I know of the existence of ConversionServiceConverterManager.StringToNameConverter
but for some reason, it doesn't seem to get triggered. Any pointers?