Skip to content

Latest commit

 

History

History
99 lines (64 loc) · 4.97 KB

File metadata and controls

99 lines (64 loc) · 4.97 KB
title description author ms.author ms.topic ms.date
Build API clients for Java
Learn how use Kiota to build API clients in Java.
jasonjoh
jasonjoh
quickstart
03/24/2023

Build API clients for Java

In this tutorial, you build a sample app in Java that calls a REST API that doesn't require authentication.

Required tools

Create a project

Use Gradle to initialize a Java application project.

gradle init --dsl groovy --test-framework junit --type java-application --project-name kiotaposts --package kiotaposts

Project configuration

In case you're adding a Kiota client to an existing project, the following configuration is required:

  • build.gradle > compileJava > sourceCompatibility set to "1.8" or later. (or equivalent if you're not using gradle)
  • build.gradle > compileJava > targetCompatibility set to "1.8" or later. (or equivalent if you're not using gradle)
  • Java Development Kit (JDK) version 17 or above.

Add dependencies

Before you can compile and run the generated API client, you need to make sure the generated source files are part of a project with the required dependencies. Your project must have a reference to the bundle package. For more information about Kiota dependencies, see the dependencies documentation.

Edit ./app/build.gradle to add the following dependencies.

Note

Find current version numbers for Kiota packages at Nexus Repository Manager.

:::code language="gradle" source="~/code-snippets/get-started/quickstart/java/app/build.gradle" id="DependenciesSnippet":::

Generate the API client

Kiota generates API clients from OpenAPI documents. Create a file named posts-api.yml and add the following.

:::code language="yaml" source="~/code-snippets/get-started/quickstart/posts-api.yml":::

This file is a minimal OpenAPI description that describes how to call the /posts endpoint in the JSONPlaceholder REST API.

You can then use the Kiota command line tool to generate the API client classes.

kiota generate -l java -c PostsClient -n kiotaposts.client -d ./posts-api.yml -o ./app/src/main/java/kiotaposts/client --ds none -s none

Tip

Add --exclude-backward-compatible if you want to reduce the size of the generated client and are not concerned about potentially source breaking changes with future versions of Kiota when updating the client.

Create the client application

The final step is to update the ./app/src/main/java/kiotaposts/App.java file that was generated as part of the console application, replacing its contents with the following code.

:::code language="java" source="~/code-snippets/get-started/quickstart/java/app/src/main/java/kiotaposts/App.java" id="ProgramSnippet":::

Note

The JSONPlaceholder REST API doesn't require any authentication, so this sample uses the AnonymousAuthenticationProvider. For APIs that require authentication, use an applicable authentication provider.

Run the application

To start the application, run the following command in your project directory.

./gradlew --console plain run

See also

(UNOFFICIAL) Community tools and libraries

Important

The Kiota Community tools and libraries are maintained and distributed by the community and are not official Microsoft tools and libraries. Microsoft makes no warranties, express or implied, with respect to the tools and libraries or their use. Use of the tools and libraries at your own risk. Microsoft shall not be liable for any damages arising out of or in connection with the use of the tools and libraries.

There are community tools and libraries to make it easy for you to get started with Kiota and to better harmonize the class path according to the rest of your stack: