In this guide we will show how work with our APIs using a simple HTTP client using Java. Note that all APIs are HTTP so it’s easy to port to a language of your choice.
To save events to Serialized we need a HTTP client (or any or the provided official clients at our Github page). In this case we use the standard Java Client from JAX-RS.
Client client = ClientBuilder.newClient(); URI apiRoot = URI.create("https://api.serialized.io");
For more information about our official Java client, visit our Github page.
2. Storing events
The most straightforward way to save an event in Serialized is to define it as a raw map. However, events are always stored in batches, to provide support for atomically saving multiple events, hence we create an event batch like this:
var todoEvents = Map.of( "events", List.of( Map.of( "eventType", "TodoAdded", "data", Map.of( "text", "Buy milk" ) ) ) );
Now we can send this event batch to Serialized and it will be stored in your project’s database under the aggregate name that you provide in the path (in this case
todo-list). You also need to provide your Serialized credentials for your project in this call.
Response response = client.target(apiRoot) .path("aggregates") .path("todo-list") .path("3070b6fb-f31b-4a8e-bc03-e22d38f4076e") .path("events") .request(MediaType.APPLICATION_JSON_TYPE) .header("Serialized-Access-Key", "<YOUR_ACCESS_KEY>") .header("Serialized-Secret-Access-Key", "<YOUR_SECRET_ACCESS_KEY>") .post(Entity.json(todoEvents));
3. Checkout the console
Login to the Serialized Console and see your event created under the Aggregates section, where you now should have an aggregate called
todo-list with an event saved.
Now that you’ve successfully saved events to your project, continue to the docs or our sample code to find out how to build projections from your events or how to make reactions that drive your application.