Skip to content

Commit cc7975d

Browse files
authored
Fix invoke http example. (dapr#230)
1 parent dad47d2 commit cc7975d

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

examples/src/main/java/io/dapr/examples/invoke/http/InvokeClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public class InvokeClient {
3131
public static void main(String[] args) {
3232
DaprClient client = (new DaprClientBuilder()).build();
3333
for (String message : args) {
34-
client.invokeService(Verb.POST, SERVICE_APP_ID, "say", message, null, String.class).block();
34+
byte[] response = client.invokeService(
35+
Verb.POST, SERVICE_APP_ID, "say", message, null, byte[].class).block();
36+
System.out.println(new String(response));
3537
}
3638
}
3739
}

examples/src/main/java/io/dapr/examples/invoke/http/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Once running, the ExposerService is now ready to be invoked by Dapr.
9797

9898
### Running the InvokeClient sample
9999

100-
The Invoke client sample uses the Dapr SDK for invoking the remote method. The main method declares a Dapr Client using the DaprClientBuilder class. Notice that this builder gets two serializer implementations in the constructor: One is for Dapr's sent and recieved objects, and second is for objects to be persisted. It needs to know the method name to invoke as well as the application id for the remote application. In `InvokeClient.java` file, you will find the `InvokeClient` class and the `main` method. See the code snippet below:
100+
The Invoke client sample uses the Dapr SDK for invoking the remote method. The main method declares a Dapr Client using the `DaprClientBuilder` class. Notice that [DaprClientBuilder](https://github.com/dapr/java-sdk/blob/master/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java) can receive two optional serializers: `withObjectSerializer()` is for Dapr's sent and received objects, and `withStateSerializer()` is for objects to be persisted. It needs to know the method name to invoke as well as the application id for the remote application. This example, we stick to the [default serializer](https://github.com/dapr/java-sdk/blob/master/sdk/src/main/java/io/dapr/serializer/DefaultObjectSerializer.java). In `InvokeClient.java` file, you will find the `InvokeClient` class and the `main` method. See the code snippet below:
101101

102102
```java
103103
public class InvokeClient {
@@ -107,14 +107,16 @@ private static final String SERVICE_APP_ID = "invokedemo";
107107
public static void main(String[] args) {
108108
DaprClient client = (new DaprClientBuilder()).build();
109109
for (String message : args) {
110-
client.invokeService(Verb.POST, SERVICE_APP_ID, "say", message, null, String.class).block();
110+
byte[] response = client.invokeService(
111+
Verb.POST, SERVICE_APP_ID, "say", message, null, byte[].class).block();
112+
System.out.println(new String(response));
111113
}
112114
}
113115
///...
114116
}
115117
```
116118

117-
The class knows the app id for the remote application. It uses the the static `Dapr.getInstance().invokeService` method to invoke the remote method defining the parameters: The verb, application id, method name, and proper data and metadata, as well as the type of the expected retun data.
119+
The class knows the app id for the remote application. It uses the the static `Dapr.getInstance().invokeService` method to invoke the remote method defining the parameters: The verb, application id, method name, and proper data and metadata, as well as the type of the expected return type. The returned payload for this method invocation is plain text and not a [JSON String](https://www.w3schools.com/js/js_json_datatypes.asp), so we expect `byte[]` to get the raw response and not try to deserialize it.
118120

119121
Execute the follow script in order to run the InvokeClient example, passing two messages for the remote method:
120122
```sh

0 commit comments

Comments
 (0)