eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – Diagrid – NPI EA (cat= Testing)
announcement - icon

In distributed systems, managing multi-step processes (e.g., validating a driver, calculating fares, notifying users) can be difficult. We need to manage state, scattered retry logic, and maintain context when services fail.

Dapr Workflows solves this via Durable Execution which includes automatic state persistence, replaying workflows after failures and built-in resilience through retries, timeouts and error handling.

In this tutorial, we'll see how to orchestrate a multi-step flow for a ride-hailing application by integrating Dapr Workflows and Spring Boot:

>> Dapr Workflows With PubSub

1. Introduction

In this article, we’re going to have a look at the GitHub API For Java library. This provides us with an object-oriented representation of the GitHib API, allowing us to easily interact with it from our Java applications.

2. Dependencies

To use the GitHub API for Java library, we need to include the latest version in our build, which is currently 1.327.

If we’re using Maven, we can include this dependency in our pom.xml file:

<dependency>
    <groupId>org.kohsuke</groupId>
    <artifactId>github-api</artifactId>
    <version>1.327</version>
</dependency>

At this point, we’re ready to start using it in our application.

3. Client Creation

In order to use the library, we first need to create a GitHub client instance. This acts as the main entrypoint for interacting with the GitHub API.

The easiest way to create this instance is to connect anonymously:

GitHub gitHub = GitHub.connectAnonymously();

This then lets us access the API without any authentication credentials. However, what we can do with this is restricted to only those features that can work in this way.

Alternatively, we can connect with credentials:

GitHub gitHub = GitHub.connect();

Doing this will attempt to determine the credentials to use in several different ways:

  • If the environment property GITHUB_OAUTH is set, then this will be used as a personal access token.
  • Otherwise, if the environment property GITHUB_JWT is set, this will be used as a JWT token.
  • Otherwise, if both GITHUB_LOGIN and GITHUB_PASSWORD are set then these will be used directly as credentials.
  • Otherwise, we’ll attempt to use the .github properties file in the user’s home directory to provide the equivalent properties. In this case, the names will be lowercase and without the GITHUB_ prefix.

If none of these values are available, creating the client will fail.

We can additionally choose to create the client by providing the credentials ourselves manually:

GitHub github = new GitHubBuilder().withOAuthToken("my_personal_token").build();
GitHub github = new GitHubBuilder().withJwtToken("my_jwt_token").build();
GitHub github = new GitHubBuilder().withPassword("my_user", "my_password").build();

If we choose to use one of these, we can then load the appropriate credentials from wherever we wish.

Also, note that the library uses these credentials only when needed. This means that if the credentials provided are invalid, we’ll only find out when we interact with the API in ways that require authentication. If we use any API methods that work in an anonymous way then these will continue to work.

4. Myself and Other Users

Once we’ve created a client, we can start to interact with the GitHub API.

If we’ve got a correctly authenticated client then we can use this to query the user that we’re authenticated as – known as Myself:

GHMyself myself = gitHub.getMyself();

We can then use this object to query the current user:

assertEquals("someone", myself.getLogin());
assertEquals("[email protected]", myself.getEmail());
assertEquals(50, myself.getFollows().size());

We’re also able to query the details of other users as well:

GHUser user = gitHub.getUser("eugenp");
assertEquals("eugenp", user.getLogin());
assertEquals(2, user.getFollows().size());

This returns a GHUser, which is the superclass of GHMyself. As such, there are certain things that we can do with the GHMyself object – representing the current authenticated user – that we can’t do with any other users. These include:

  • Managing public keys
  • Managing email addresses
  • Managing organisation memberships

However, anything that expects a GHUser can also accept a GHMyself as well.

5. Repositories

We can also work with repositories, as well as with users. This will include the ability to query the list of repositories owned by a user, but also to access the contents of a repository and even make changes to it.

Note that all repositories in GitHub are owned by exactly one user, and so we need to know the username as well as the repository name to be able to correctly access them.

5.1. Listing Repositories

If we’ve got a GHUser object – or GHMyself – then we can use this to get the repositories that this user owns. This is achieved using listRepositories():

PagedIterable<GHRepository> repositories = user.listRepositories();

This gives us a PagedIterable since there might be a very large number of these repositories. By default, this works with a page size of 30, but we can specify this when listing the repositories if needed:

PagedIterable<GHRepository> repositories = user.listRepositories(50);

This then gives us a number of ways to access the actual repositories. We can convert it to Array, List or Set types containing the entire collection of repositories:

Array<GHRepository> repositoriesArray = repositories.toArray();
List<GHRepository> repositoriesList = repositories.toList();
Set<GHRepository> repositoriesSet = repositories.toSet();

However, these will fetch everything up front – which can be expensive if there are a very large number. For example, the Microsoft user currently has 6,688 repositories. At 30 repositories per page, this would take 223 API calls to collect the full list.

Alternatively we can obtain an iterator over the repositories. This will then only make API calls on demand, allowing us much more efficient access to the collection:

Iterator<GHRepository> repositoriesSet = repositories.toIterator();

Even easier though, the PagedIterable is itself an Iterable and can be used directly anywhere this is applicable – for example, in an enhanced-for loop:

Set<String> names = new HashSet<>();
for (GHRepository ghRepository : user.listRepositories()) {
    names.add(ghRepository.getName());
}

This simply iterates over every returned repository and extracts their names. Because we’re using the PagedIterable this will act like a normal Iterable but will make API calls behind the scenes only when necessary.

5.2. Directly Accessing Repositories

As well as listing repositories, we can directly access them by name. If we’ve got the appropriate GHUser then we only need the repository name:

GHRepository repository = user.getRepository("tutorials");

Alternatively, we can access the repository directly from our client. In this case, we need the full name comprising of the username and repository name combined into a single string:

GHRepository repository = gitHub.getRepository("eugenp/tutorials");

This then gives us the exact same GHRepository object as if we’d navigated through the GHUser object.

5.3. Working With Repositories

Once we’ve got a GHRepository object, we can start to interact with this directly.

At the simplest level, this allows us to retrieve repository details such as its name, owner, creation date, and more.

String name = repository.getName();
String fullName = repository.getFullName();
GHUser owner = repository.getOwner();
Date created = repository.getCreatedAt();

However, we can also query the contents of the repository. We can do this by treating it as a Git repository – allowing access to branches, tags, commits, and so on:

String defaultBranch = repository.getDefaultBranch();
GHBranch branch = repository.getBranch(defaultBranch);
String branchHash = branch.getSHA1();

GHCommit commit = repository.getCommit(branchHash);
System.out.println(commit.getCommitShortInfo().getMessage());

Alternatively, if we know the full names then we can access the full contents of files:

String defaultBranch = repository.getDefaultBranch();
GHContent file = repository.getFileContent("pom.xml", defaultBranch);

String fileContents = IOUtils.toString(file.read(), Charsets.UTF_8);

If they exist, we can also access certain special files – specifically, the readme and license:

GHContent readme = repository.getReadme();
GHContent license = repository.getLicenseContent();

These work exactly the same as accessing the file directly, but without needing to know the filename. In particular, GitHub supports a range of different filenames for these concepts and this will return the correct one.

5.4. Manipulating Repositories

As well as reading repository content, we can also make changes to them.

This can include updating the configuration of the repository itself, allowing us to do things like changing the description, home page, visibility, and so on:

repository.setDescription("A new description");
repository.setVisibility(GHRepository.Visibility.PRIVATE);

We’re also able to fork other repositories into our own account:

repository.createFork().name("my_fork").create();

We can also create branches, tags, pull requests and other things:

repository.createRef("new-branch", oldBranch.getSHA1());
repository.createTag("new-tag", "This is a tag", branch.getSHA1(), "commit");
repository.createPullRequest("new-pr", "from-branch", "to-branch", "Description of the pull request");

It’s even possible to create entire commits if we need to, though this is much more involved.

6. Conclusion

In this article, we’ve taken a very brief look at the GitHub API For Java library. There’s much more that can be done using this library. Next time you need to work with the GitHub API, why not give it a try.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – LS – NPI (cat=REST)
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

eBook Jackson – NPI EA – 3 (cat = Jackson)