Announcing the public preview of Microsoft Graph Java SDK v3

Vincent Biret

The next version of the Microsoft Graph Java/Android SDK is now available for preview! Leverage new modern development features when integrating your application with Microsoft Graph.

New features and improvements

This new version of the SDK includes the following improvements:

  • New and improved authentication API based on the Azure Identity library. Use a single API to authenticate against Azure services and Microsoft Graph. Azure Identity adds support for device code flow and client certificate credentials grant flow that were not previously supported.
  • Modern CompletableFuture API surface for asynchronous calls. Execute Microsoft Graph calls in the background without blocking the main thread using a standardized API. This allows you to deliver a fluid user experience without compromising on the code maintainability.
  • Modern date and time API. Use OffsetDateTime instead of Calendar covering a broader set of scenarios and providing more consistent APIs.
  • New fluent API and optional parameters support for OData methods. Take advantage of the full set of methods available on Microsoft Graph.
  • Performance and reliability improvements. Thanks to reduced memory and CPU consumption, the SDK delivers a faster experience to your end users.
  • Batch support improvements. Execute Microsoft Graph JSON batches from a streamlined API, reducing the code needed in your application.

Try out the new version today!

Try out this new preview version today and let us know what you think! See the following instructions to add the new SDK to your project.

Gradle

Edit the “build.gradle” file of your project to contain the following entries

In the “repositories” section:

maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }

In the “dependencies” section

implementation 'com.microsoft.graph:microsoft-graph-core:2.0.0-SNAPSHOT'
implementation 'com.microsoft.graph:microsoft-graph:3.0.0-SNAPSHOT'

Maven

In the “pom.xml” file under the project node:

<profiles>
  <profile>
     <id>allow-snapshots</id>
     <activation><activeByDefault>true</activeByDefault></activation>
     <repositories>
       <repository>
         <id>snapshots-repo</id>
         <url>https://oss.sonatype.org/content/repositories/snapshots</url>
         <releases><enabled>false</enabled></releases>
         <snapshots><enabled>true</enabled></snapshots>
       </repository>
     </repositories>
  </profile>
</profiles>

In the “pom.xml” file under the dependencies node:

<dependency>
  <groupId>com.microsoft.graph</groupId>
  <artifactId>microsoft-graph</artifactId>
  <version>3.0.0-SNAPSHOT</version>
</dependency>

Major improvements showcase

This section includes examples of some of the improvements in this new version.

Device code flow support

Thanks to Azure Identity, the Microsoft Graph Java SDK now supports device code flow. This flow is ideal for devices that have limited input capabilities like IoT devices, TVs, wearables, and more.

To use this new authentication flow and query Microsoft Graph with the Java SDK, simply add the following lines to your application.

final DeviceCodeCredential deviceCodeCredential = new DeviceCodeCredentialBuilder()
    .clientId(CLIENT_ID) // the client id of your application registration
    .challengeConsumer(challenge -> {
                        // lets user know of the challenge
                        System.out.println(challenge.getMessage());
                    })
    .build();

final TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(SCOPES, deviceCodeCredential);
final GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();

CompletableFuture API surface

CompletableFuture are a standard and simple way of deferring asynchronous code to another thread. By doing so, you avoid blocking the main thread to wait on some network, IO or CPU intensive operation. This is especially important when building applications with a Graphical User Interface (GUI) to avoid having the interface freeze on each call allowing you to deliver a better user experience. This is also important for backend server applications to allow the main thread to continue working and support more incoming customers requests instead on waiting for completion, allowing for more throughput.

The new asynchronous API surface in the SDK is denoted with an “Async” suffix and it automatically runs the code in a background thread pool for you.

graphClient.me().drive().buildRequest().getAsync().thenApply(result -> {
    System.out.println("Found Drive " + result.id); // this method will be called once the request is completed
}); // the main thread continues execution after starting the request

Fluent API for methods parameters & optional parameters support

The new SDK now provides a ParameterSet class for each method allowing developers to fine-tune the parameters to provide the OData method (actions & functions). This new ParameterSet object also allows developers to use OData methods overloads.

For example:

graphClient.me().drive().items("{id}").workbook().worksheets("{id|name}")
.range(WorkbookWorksheetRangeParameterSet.newBuilder().withAddress("A1:B2").build());

This approach allows developers to specific any parameter by using the “withParameterName” methods on the ParameterSet builder.

Next steps

Check out this GitHub post to access a detailed upgrade guide. The upgrade guide contains further details of these improvements.

Credits

This blog post was co-authored with Maisa Rissi, Microsoft Graph SDKs Program Manager.

Feedback usabilla icon