How to get user details giver user ID

I’m writing a Confluence plugin and I need to get user full name given user ID. I found UserAccessor interface, but whenever I try to use it in my components I get NoSuchBeanDefinitionException:


Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.atlassian.confluence.user.UserAccessor' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

I saw similar question raised before, but I couldn’t find any clear resolution. Should I be using some other bean to get user details from?

Hi Igor,

We recommend using our public API to get user details. In this case, what you’ll want to use is the com.atlassian.confluence.api.service.people.PersonService.PersonFinder to retrieve a Person object based on their user key. From there, you can get the full name for that person.

Sample code:

Optional<Person> person = getPersonService().find().withUserKey(userKey).fetch();
            if (person.isPresent()) {
                return person.get().getDisplayName();
            }

Please note that invoking fetch on the PersonFinder in the PersonService will return an empty Optional if no person matching the userkey could be found.

I hope you find this helpful!

Kind regards,

Sarah

Hi @bandersen!

Thanks for you suggestions, but I’m still getting similar error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.atlassian.confluence.api.service.people.PersonService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

How do I get an instance of PersonService?

Somehow it works if I specify @ExportAsService annotation on my REST resource and I’m not sure if that has to be that way.

Also found something weird. When I run this code:

personService
    .find()
    .fetchMany(new SimplePageRequest(0, 1000))
    .getResults()
    .forEach((person) -> {
        System.out.println("##### fetchMany: " + person);
    });

I’m getting

java.lang.ClassCastException: com.atlassian.crowd.embedded.atlassianuser.EmbeddedCrowdUser cannot be cast to com.atlassian.confluence.user.ConfluenceUser