Skip to content

Commit 2bb8ce0

Browse files
authored
Merge pull request #4933 from mcruzdev/raw-message-java
Add documentation about rawPayload for Java
2 parents a6014dc + 30938cb commit 2bb8ce0

File tree

1 file changed

+56
-0
lines changed
  • daprdocs/content/en/developing-applications/building-blocks/pubsub

1 file changed

+56
-0
lines changed

daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,36 @@ $app->run(function(\DI\FactoryInterface $factory) {
101101

102102
{{% /tab %}}
103103

104+
{{% tab "Java" %}}
105+
106+
```java
107+
@RestController
108+
@PathMapping("/publish")
109+
public class PublishController {
110+
111+
@Inject
112+
DaprClient client;
113+
114+
@PostMapping
115+
public void sendRawMessage() {
116+
117+
Map<String, String> metadata = new HashMap<>();
118+
metatada.put("content-type", "application/json");
119+
metadata.put("rawPayload", "true");
120+
121+
Message message = new Message(UUID.random().toString(), "Hello from Dapr");
122+
123+
client.publishEvent(
124+
"pubsub", // pubsub name
125+
"messages", // topic name
126+
message, // message data
127+
metadata) // metadata
128+
.block(); // wait for completion
129+
}
130+
}
131+
```
132+
{{% /tab %}}
133+
104134
{{< /tabpane >}}
105135

106136
## Subscribing to raw messages
@@ -216,6 +246,32 @@ $app->start();
216246
```
217247
{{% /tab %}}
218248

249+
{{% tab "Java" %}}
250+
```java
251+
@RequestMapping("/consumer")
252+
@RestController
253+
public class MessageConsumerController {
254+
255+
@PostMapping
256+
@ResponseStatus(HttpStatus.OK)
257+
@Topic(pubsubName = "pubsub", name = "messages", metadata = "{\"rawPayload\":\"true\", \"content-type\": \"application/json\"}")
258+
public void consume(@RequestBody Message message) {
259+
System.out.println("Message received: " + message);
260+
}
261+
262+
@PostMapping
263+
@ResponseStatus(HttpStatus.OK)
264+
@Topic(pubsubName = "pubsub", name = "another-topic", metadata = """
265+
{"rawPayload": "true", "content-type": "application/json"}
266+
""") // Using Java 15 text block
267+
public void consumeAnother(@RequestBody Message message) {
268+
System.out.println("Message received: " + message);
269+
}
270+
}
271+
272+
```
273+
{{% /tab %}}
274+
219275
{{< /tabpane >}}
220276

221277
## Declaratively subscribe to raw events

0 commit comments

Comments
 (0)