Get content / link to content by id programmatically

Hey, I want to get the result of what the rest endpointrest/api/content/{id} gives me, but programmatically.
Is there any way getting these informations without calling the rest api? Making the request seems kind of redundant as I’m inside the application anyway.
I don’t even need the whole thing, getting the _link section of a content would be enough, or more specifically, the link to the webui.

Hi @PhilipFeldmann you can use the ContentService from our java api.

import com.atlassian.confluence.api.model.content.id.ContentId;
import com.atlassian.confluence.api.model.link.Link;
import com.atlassian.confluence.api.model.link.LinkType;
import com.atlassian.confluence.api.service.content.ContentService;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class SampleContentComponent {

    private final ContentService contentService;

    public SampleContentComponent(@ComponentImport ContentService contentService) {
        this.contentService = contentService;
    }

    public Optional<Link> getWebUILink(long id) {
        return contentService.find()
                .withId(ContentId.of(id))
                .fetch()
                .map(content -> content.getLinks().get(LinkType.WEB_UI));
    }
}

I hope this helps, let me know if you need more info.

1 Like