πŸ§‘β€πŸš€Micronaut Serialization

How to serialize Result objects with Micronaut

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.

How to Use this Add-On

Add this Maven dependency to your build:

Group ID
Artifact ID
Latest Version

com.leakyabstractions

result-micronaut-serde

Test Scenario

Let's start by creating a record ApiOperation containing one ordinary and one Result field.

/** Represents an API operation */
@Serdeable
public record ApiOperation(String name, Result<String, String> result) {
}

Problem Overview

We will take a look at what happens when we try to serialize and deserialize ApiOperation objects with Micronaut.

Serialization Problem

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:

Deserialization Problem

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.

Solution Implementation

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.

Adding the Serde Imports to the Classpath

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.

Serializing Results

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:

Deserializing Results

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.

Conclusion

We learned how to serialize and deserialize Result objects using Micronaut, demonstrating how the provided @SerdeImport enables Micronaut to treat Results as Serdeable objects.

Last updated

Was this helpful?