|
| 1 | +--- |
| 2 | +type: docs |
| 3 | +title: "Dapr Java SDK" |
| 4 | +linkTitle: "Java" |
| 5 | +weight: 1000 |
| 6 | +description: Java SDK packages for developing Dapr applications |
| 7 | +cascade: |
| 8 | + github_repo: https://github.com/dapr/java-sdk |
| 9 | + github_subdir: daprdocs/content/en/java-sdk-docs |
| 10 | + path_base_for_github_subdir: content/en/developing-applications/sdks/java/ |
| 11 | + github_branch: master |
| 12 | +--- |
| 13 | + |
| 14 | +Dapr offers a variety of packages to help with the development of Java applications. Using them you can create Java clients, servers, and virtual actors with Dapr. |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- [Dapr CLI]({{% ref install-dapr-cli.md %}}) installed |
| 19 | +- Initialized [Dapr environment]({{% ref install-dapr-selfhost.md %}}) |
| 20 | +- JDK 11 or above - the published jars are compatible with Java 8: |
| 21 | + - [AdoptOpenJDK 11 - LTS](https://adoptopenjdk.net/) |
| 22 | + - [Oracle's JDK 15](https://www.oracle.com/java/technologies/javase-downloads.html) |
| 23 | + - [Oracle's JDK 11 - LTS](https://www.oracle.com/java/technologies/javase-jdk11-downloads.html) |
| 24 | + - [OpenJDK](https://openjdk.java.net/) |
| 25 | +- Install one of the following build tools for Java: |
| 26 | + - [Maven 3.x](https://maven.apache.org/install.html) |
| 27 | + - [Gradle 6.x](https://gradle.org/install/) |
| 28 | + |
| 29 | +## Import Dapr's Java SDK |
| 30 | + |
| 31 | +Next, import the Java SDK packages to get started. Select your preferred build tool to learn how to import. |
| 32 | + |
| 33 | +{{< tabpane text=true >}} |
| 34 | + |
| 35 | +{{% tab header="Maven" %}} |
| 36 | +<!--Maven--> |
| 37 | + |
| 38 | +For a Maven project, add the following to your `pom.xml` file: |
| 39 | + |
| 40 | +```xml |
| 41 | +<project> |
| 42 | + ... |
| 43 | + <dependencies> |
| 44 | + ... |
| 45 | + <!-- Dapr's core SDK with all features, except Actors. --> |
| 46 | + <dependency> |
| 47 | + <groupId>io.dapr</groupId> |
| 48 | + <artifactId>dapr-sdk</artifactId> |
| 49 | + <version>1.16.0</version> |
| 50 | + </dependency> |
| 51 | + <!-- Dapr's SDK for Actors (optional). --> |
| 52 | + <dependency> |
| 53 | + <groupId>io.dapr</groupId> |
| 54 | + <artifactId>dapr-sdk-actors</artifactId> |
| 55 | + <version>1.16.0</version> |
| 56 | + </dependency> |
| 57 | + <!-- Dapr's SDK integration with SpringBoot (optional). --> |
| 58 | + <dependency> |
| 59 | + <groupId>io.dapr</groupId> |
| 60 | + <artifactId>dapr-sdk-springboot</artifactId> |
| 61 | + <version>1.16.0</version> |
| 62 | + </dependency> |
| 63 | + ... |
| 64 | + </dependencies> |
| 65 | + ... |
| 66 | +</project> |
| 67 | +``` |
| 68 | +{{% /tab %}} |
| 69 | + |
| 70 | +{{% tab header="Gradle" %}} |
| 71 | +<!--Gradle--> |
| 72 | + |
| 73 | +For a Gradle project, add the following to your `build.gradle` file: |
| 74 | + |
| 75 | +```java |
| 76 | +dependencies { |
| 77 | +... |
| 78 | + // Dapr's core SDK with all features, except Actors. |
| 79 | + compile('io.dapr:dapr-sdk:1.16.0') |
| 80 | + // Dapr's SDK for Actors (optional). |
| 81 | + compile('io.dapr:dapr-sdk-actors:1.16.0') |
| 82 | + // Dapr's SDK integration with SpringBoot (optional). |
| 83 | + compile('io.dapr:dapr-sdk-springboot:1.16.0') |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +{{% /tab %}} |
| 88 | + |
| 89 | +{{< /tabpane >}} |
| 90 | + |
| 91 | +If you are also using Spring Boot, you may run into a common issue where the `OkHttp` version that the Dapr SDK uses conflicts with the one specified in the Spring Boot _Bill of Materials_. |
| 92 | + |
| 93 | +You can fix this by specifying a compatible `OkHttp` version in your project to match the version that the Dapr SDK uses: |
| 94 | + |
| 95 | +```xml |
| 96 | +<dependency> |
| 97 | + <groupId>com.squareup.okhttp3</groupId> |
| 98 | + <artifactId>okhttp</artifactId> |
| 99 | + <version>1.16.0</version> |
| 100 | +</dependency> |
| 101 | +``` |
| 102 | + |
| 103 | +## Try it out |
| 104 | + |
| 105 | +Put the Dapr Java SDK to the test. Walk through the Java quickstarts and tutorials to see Dapr in action: |
| 106 | + |
| 107 | +| SDK samples | Description | |
| 108 | +| ----------- | ----------- | |
| 109 | +| [Quickstarts]({{% ref quickstarts %}}) | Experience Dapr's API building blocks in just a few minutes using the Java SDK. | |
| 110 | +| [SDK samples](https://github.com/dapr/java-sdk/tree/master/examples) | Clone the SDK repo to try out some examples and get started. | |
| 111 | + |
| 112 | +```java |
| 113 | +import io.dapr.client.DaprClient; |
| 114 | +import io.dapr.client.DaprClientBuilder; |
| 115 | + |
| 116 | +try (DaprClient client = (new DaprClientBuilder()).build()) { |
| 117 | + // sending a class with message; BINDING_OPERATION="create" |
| 118 | + client.invokeBinding(BINDING_NAME, BINDING_OPERATION, myClass).block(); |
| 119 | + |
| 120 | + // sending a plain string |
| 121 | + client.invokeBinding(BINDING_NAME, BINDING_OPERATION, message).block(); |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +- For a full guide on output bindings visit [How-To: Output bindings]({{% ref howto-bindings.md %}}). |
| 126 | +- Visit [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/bindings/http) for code samples and instructions to try out output bindings. |
| 127 | + |
| 128 | +## Available packages |
| 129 | + |
| 130 | +<div class="card-deck"> |
| 131 | + <div class="card"> |
| 132 | + <div class="card-body"> |
| 133 | + <h5 class="card-title"><b>Client</b></h5> |
| 134 | + <p class="card-text">Create Java clients that interact with a Dapr sidecar and other Dapr applications.</p> |
| 135 | + <a href="{{% ref java-client %}}" class="stretched-link"></a> |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + <div class="card"> |
| 139 | + <div class="card-body"> |
| 140 | + <h5 class="card-title"><b>Workflow</b></h5> |
| 141 | + <p class="card-text">Create and manage workflows that work with other Dapr APIs in Java.</p> |
| 142 | + <a href="{{% ref workflow %}}" class="stretched-link"></a> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | +</div> |
0 commit comments