ð§âðMicronaut Serialization
How to serialize Result objects with Micronaut
Last updated
How to serialize Result objects with Micronaut
Last updated
Copyright 2024 Guillermo Calvo
When using Result objects with Micronaut, we might run into some problems. The Micronaut serialization support for Result solves them by making Micronaut treat results as Serdeable
(so they can be serialized and deserialized).
Micronaut is a modern, JVM-based framework for building lightweight microservices and serverless applications. It focuses on fast startup times and low memory usage. Although not as widely adopted as Spring Boot, it has gained popularity for its performance and innovative features.
Add this Maven dependency to your build:
Group ID | Artifact ID | Latest Version |
---|---|---|
Maven Central provides snippets for different build tools to declare this dependency.
Let's start by creating a record ApiOperation
containing one ordinary and one Result field.
We will take a look at what happens when we try to serialize and deserialize ApiOperation
objects with Micronaut.
Now, let's create a Micronaut controller that returns an instance of ApiOperation
containing a successful result.
And finally, let's run the application and try the /operations/last
endpoint we just created.
We'll see that we get a Micronaut CodecException
caused by a SerdeException
.
Although this may look strange, it's actually what we should expect. Even though we annotated ApiOperation
as @Serdeable
, Micronaut doesn't know how to serialize result objects yet, so the data structure cannot be serialized.
This is Micronaut's default serialization behavior. But we'd like to serialize the result
field like this:
Now, let's reverse our previous example, this time trying to receive an ApiOperation
as the body of a POST
request.
We'll see that now we get an IntrospectionException
. Let's inspect the stack trace.
This behavior again makes sense. Essentially, Micronaut cannot create new result objects, because Result
is not annotated as @Introspected
or @Serdeable
.
What we want, is for Micronaut to treat Result values as JSON objects that contain either a success
or a failure
value. Fortunately, there's an easy way to solve this problem.
All we need to do now is add Result-Micronaut-Serde as a Maven dependency. Once the @SerdeImport
is in the classpath, all functionality is available for all normal Micronaut operations.
Now, let's try and serialize our ApiOperation
object again.
If we look at the serialized response, we'll see that this time the result
field contains a success
field.
Next, we can try serializing a failed result.
We can verify that the serialized response contains a non-null failure
value and a null success
value:
Now, let's repeat our tests for deserialization. If we read our ApiOperation
again, we'll see that we no longer get an IntrospectionException
.
Finally, let's repeat the test again, this time with a failed result. We'll see that yet again we don't get an exception, and in fact, have a failed result.
We learned how to serialize and deserialize Result objects using Micronaut, demonstrating how the provided @SerdeImport
enables Micronaut to treat Results as Serdeable
objects.
The full source code for the examples is available on GitHub.
com.leakyabstractions
result-micronaut-serde