diff --git a/examples/restful-ws-quarkus/README.md b/examples/restful-ws-quarkus/README.md index 6b06b99aa..2b3e7b275 100644 --- a/examples/restful-ws-quarkus/README.md +++ b/examples/restful-ws-quarkus/README.md @@ -1,7 +1,11 @@ # Cloudevents Restful WS Quarkus example This sample application has a `/users` REST endpoint in which you can manage the different users. -The way to create users is through CloudEvents. Here is an example POST: +The way to create users is through CloudEvents. + +## Example requests + +### [Binary Content mode](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/bindings/http-protocol-binding.md#31-binary-content-mode) ```shell script curl -v http://localhost:8080/users \ @@ -30,6 +34,29 @@ curl -v http://localhost:8080/users \ < Location: http://localhost:8080/users ``` +### [Structured Content mode](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/bindings/http-protocol-binding.md#32-structured-content-mode) + +```shell script +curl -v http://localhost:8080/users \ + -H "Content-Type: application/cloudevents+json" \ + -d @examples/user_structured.json + +> POST /users HTTP/1.1 +> Host: localhost:8080 +> User-Agent: curl/7.82.0 +> Accept: */* +> Content-Type: application/cloudevents+json +> Content-Length: 290 +> + +< HTTP/1.1 201 Created +< Location: http://localhost:8081/users +< content-length: 0 +< +``` + +### Generated events + In order to also show how to create and send CloudEvents, generated events will be periodically sent each 2 seconds through HTTP to the same endpoint using a REST client. diff --git a/examples/restful-ws-quarkus/examples/user_structured.json b/examples/restful-ws-quarkus/examples/user_structured.json new file mode 100644 index 000000000..0ff09f322 --- /dev/null +++ b/examples/restful-ws-quarkus/examples/user_structured.json @@ -0,0 +1,13 @@ +{ + "specversion" : "1.0", + "type" : "User", + "source": "io.cloudevents.examples/user", + "id": "536808d3-88be-4077-9d7a-a3f162705f78", + "subject": "SUBJ-0001", + "data" : { + "username": "jsmith", + "firstName": "John", + "lastName": "Smith", + "age": 37 + } +} \ No newline at end of file diff --git a/examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/client/UserClient.java b/examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/client/UserClient.java index f225937d8..cd2f025e6 100644 --- a/examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/client/UserClient.java +++ b/examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/client/UserClient.java @@ -1,11 +1,12 @@ package io.cloudevents.examples.quarkus.client; +import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; import io.cloudevents.CloudEvent; +import io.cloudevents.jackson.JsonFormat; + import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; @Path("/users") @@ -13,8 +14,11 @@ public interface UserClient { // This will emit binary encoded events. - // To use structured JSON encoding use @Produces(JsonFormat.CONTENT_TYPE). + // To use structured JSON encoding use @Consumes(JsonFormat.CONTENT_TYPE). + @POST + void emitBinary(CloudEvent event); + @POST - @Produces(MediaType.APPLICATION_JSON) - void emit(CloudEvent event); + @Consumes(JsonFormat.CONTENT_TYPE) + void emitStructured(CloudEvent event); } diff --git a/examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/client/UserEventsGenerator.java b/examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/client/UserEventsGenerator.java index 0c3285ced..6b79d6b9c 100644 --- a/examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/client/UserEventsGenerator.java +++ b/examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/client/UserEventsGenerator.java @@ -19,7 +19,6 @@ import io.cloudevents.core.data.PojoCloudEventData; import io.cloudevents.examples.quarkus.model.User; import io.quarkus.scheduler.Scheduled; -import io.smallrye.mutiny.Uni; @ApplicationScoped public class UserEventsGenerator { @@ -38,8 +37,13 @@ public class UserEventsGenerator { @Scheduled(every="2s") public void init() { CloudEvent event = createEvent(userCount++); - LOGGER.info("try to emit user: {}", event.getId()); - userClient.emit(event); + if(userCount % 2 == 0) { + LOGGER.info("try to emit binary event for user: {}", event.getId()); + userClient.emitBinary(event); + } else { + LOGGER.info("try to emit structured event for user: {}", event.getId()); + userClient.emitStructured(event); + } } private CloudEvent createEvent(long id) { diff --git a/examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/resources/UserResource.java b/examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/resources/UserResource.java index 4af33d0cb..39c0dec46 100644 --- a/examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/resources/UserResource.java +++ b/examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/resources/UserResource.java @@ -19,7 +19,7 @@ @Path("/users") @Consumes({MediaType.APPLICATION_JSON, JsonFormat.CONTENT_TYPE}) -@Produces({MediaType.APPLICATION_JSON}) +@Produces(MediaType.APPLICATION_JSON) public class UserResource { private static final Logger LOGGER = LoggerFactory.getLogger(UserResource.class);