{"id":5901,"date":"2021-02-05T07:45:03","date_gmt":"2021-02-05T15:45:03","guid":{"rendered":"https:\/\/officedevblogs.wpengine.com\/?p=5901"},"modified":"2021-02-05T07:45:03","modified_gmt":"2021-02-05T15:45:03","slug":"announcing-the-public-preview-of-microsoft-graph-java-sdk-v3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/announcing-the-public-preview-of-microsoft-graph-java-sdk-v3\/","title":{"rendered":"Announcing the public preview of Microsoft Graph Java SDK v3"},"content":{"rendered":"<p>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.<\/p>\n<h3>New features and improvements<\/h3>\n<p>This new version of the SDK includes the following improvements:<\/p>\n<ul>\n<li>New and improved authentication API based on the <a href=\"https:\/\/docs.microsoft.com\/en-us\/java\/api\/overview\/azure\/identity-readme?view=azure-java-stable\">Azure Identity<\/a> library. Use a single API to authenticate against Azure services and Microsoft Graph. Azure Identity adds support for <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/develop\/v2-oauth2-device-code\">device code flow<\/a> and <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/develop\/active-directory-certificate-credentials\">client certificate credentials grant flow<\/a> that were not previously supported.<\/li>\n<li>Modern <strong>CompletableFuture<\/strong> 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.<\/li>\n<li>Modern date and time API. Use <strong>OffsetDateTime<\/strong> instead of <strong>Calendar<\/strong> covering a broader set of scenarios and providing more consistent APIs.<\/li>\n<li>New fluent API and optional parameters support for OData methods. Take advantage of the full set of methods available on Microsoft Graph.<\/li>\n<li>Performance and reliability improvements. Thanks to reduced memory and CPU consumption, the SDK delivers a faster experience to your end users.<\/li>\n<li>Batch support improvements. Execute <a href=\"https:\/\/docs.microsoft.com\/en-us\/graph\/json-batching\">Microsoft Graph JSON batches<\/a> from a streamlined API, reducing the code needed in your application.<\/li>\n<\/ul>\n<h3>Try out the new version today!<\/h3>\n<p>Try out this new preview version today and <a href=\"https:\/\/github.com\/microsoftgraph\/msgraph-sdk-java\/issues\/new\">let us know what you think<\/a>! See the following instructions to add the new SDK to your project.<\/p>\n<h4>Gradle<\/h4>\n<p>Edit the \u201cbuild.gradle\u201d file of your project to contain the following entries<\/p>\n<p>In the &#8220;repositories&#8221; section:<\/p>\n<pre>maven { url 'https:\/\/oss.sonatype.org\/content\/repositories\/snapshots' }<\/pre>\n<p>In the &#8220;dependencies&#8221; section<\/p>\n<pre>implementation 'com.microsoft.graph:microsoft-graph-core:2.0.0-SNAPSHOT'\nimplementation 'com.microsoft.graph:microsoft-graph:3.0.0-SNAPSHOT'<\/pre>\n<h4>Maven<\/h4>\n<p>In the \u201cpom.xml\u201d file under the project node:<\/p>\n<pre>&lt;profiles&gt;\n \u00a0&lt;profile&gt;\n \u00a0\u00a0\u00a0 &lt;id&gt;allow-snapshots&lt;\/id&gt;\n \u00a0\u00a0\u00a0 &lt;activation&gt;&lt;activeByDefault&gt;true&lt;\/activeByDefault&gt;&lt;\/activation&gt;\n \u00a0\u00a0\u00a0 &lt;repositories&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0 &lt;repository&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0&lt;id&gt;snapshots-repo&lt;\/id&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;url&gt;https:\/\/oss.sonatype.org\/content\/repositories\/snapshots&lt;\/url&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;releases&gt;&lt;enabled&gt;false&lt;\/enabled&gt;&lt;\/releases&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;snapshots&gt;&lt;enabled&gt;true&lt;\/enabled&gt;&lt;\/snapshots&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/repository&gt;\n \u00a0\u00a0\u00a0 &lt;\/repositories&gt;\n  &lt;\/profile&gt;\n&lt;\/profiles&gt;<\/pre>\n<p>In the \u201cpom.xml\u201d file under the dependencies node:<\/p>\n<pre>&lt;dependency&gt;\n  &lt;groupId&gt;com.microsoft.graph&lt;\/groupId&gt;\n  &lt;artifactId&gt;microsoft-graph&lt;\/artifactId&gt;\n  &lt;version&gt;3.0.0-SNAPSHOT&lt;\/version&gt;\n&lt;\/dependency&gt;<\/pre>\n<h3>Major improvements showcase<\/h3>\n<p>This section includes examples of some of the improvements in this new version.<\/p>\n<h4>Device code flow support<\/h4>\n<p>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.<\/p>\n<p>To use this new authentication flow and query Microsoft Graph with the Java SDK, simply add the following lines to your application.<\/p>\n<pre>final DeviceCodeCredential deviceCodeCredential = new DeviceCodeCredentialBuilder()\n \u00a0\u00a0 .clientId(CLIENT_ID) \/\/ the client id of your application registration\n \u00a0\u00a0 .challengeConsumer(challenge -&gt; {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ lets user know of the challenge\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(challenge.getMessage());\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 })\n \u00a0\u00a0 .build();\n\nfinal TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(SCOPES, deviceCodeCredential);\nfinal GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();<\/pre>\n<h4>CompletableFuture API surface<\/h4>\n<p><strong>CompletableFuture<\/strong> 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.<\/p>\n<p>The new asynchronous API surface in the SDK is denoted with an \u201cAsync\u201d suffix and it automatically runs the code in a background thread pool for you.<\/p>\n<pre>graphClient.me().drive().buildRequest().getAsync().thenApply(result -&gt; {\n  \u00a0 System.out.println(\"Found Drive \" + result.id); \/\/ this method will be called once the request is completed\n}); \/\/ the main thread continues execution after starting the request<\/pre>\n<h4>Fluent API for methods parameters &amp; optional parameters support<\/h4>\n<p>The new SDK now provides a <strong>ParameterSet<\/strong> class for each method allowing developers to fine-tune the parameters to provide the OData method (actions &amp; functions). This new <strong>ParameterSet<\/strong> object also allows developers to use OData methods overloads.<\/p>\n<p>For example:<\/p>\n<pre>graphClient.me().drive().items(\"{id}\").workbook().worksheets(\"{id|name}\")\n.range(WorkbookWorksheetRangeParameterSet.newBuilder().withAddress(\"A1:B2\").build());<\/pre>\n<p>This approach allows developers to specific any parameter by using the \u201cwithParameterName\u201d methods on the <strong>ParameterSet <\/strong>builder.<\/p>\n<h3>Next steps<\/h3>\n<p>Check out <a href=\"https:\/\/github.com\/microsoftgraph\/msgraph-sdk-java\/issues\/628\">this GitHub post<\/a> to access a detailed <em>upgrade guide<\/em>. The <em>upgrade guide<\/em> contains further details of these improvements.<\/p>\n<h3>Credits<\/h3>\n<p>This blog post was co-authored with <a href=\"https:\/\/www.linkedin.com\/in\/maisarissi\/\">Maisa Rissi<\/a>, Microsoft Graph SDKs Program Manager.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":69092,"featured_media":25159,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3,5,128],"tags":[],"class_list":["post-5901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-graph","category-microsoft-identity-platform","category-microsoft-teams"],"acf":[],"blog_post_summary":"<p>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.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/5901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/69092"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=5901"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/5901\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/25159"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=5901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=5901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=5901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}