Serdeable (Micronaut Annotation)
import io.micronaut.serde.annotation.Serdeable;
Purpose
The @Serdeable
annotation is part of the Micronaut Serialization module (micronaut-serde
). Its primary function is to mark a Java class (like a POJO, record, etc.) as eligible for serialization (converting to formats like JSON) and deserialization (converting from formats like JSON back to an object) using Micronaut's compile-time, reflection-free mechanism.
When a class is annotated with @Serdeable
, the Micronaut compiler generates the necessary metadata (bean introspection data) at build time to handle its serialization and deserialization efficiently, without relying on runtime reflection.
Necessity for API Objects
The requirement to use @Serdeable
depends on the JSON serialization library configured in your Micronaut project:
1. Using Micronaut Serialization (micronaut-serde
)
- Yes,
@Serdeable
(or@SerdeImport
for external types) is generally required. - Micronaut Serialization operates on an "opt-in" basis for security and performance. It only processes types explicitly marked for serialization.
- If you use
micronaut-serde
(e.g., by includingio.micronaut.serde:micronaut-serde-jackson
) and try to serialize/deserialize an object not annotated with@Serdeable
, you will likely encounter aSerdeException
orIntrospectionException
. - This approach offers benefits like:
- Improved performance (reflection-free).
- Better GraalVM native image compatibility.
- Reduced potential security attack surface by not serializing arbitrary types by default.
- Compile-time checks for serialization logic.
2. Using Micronaut Jackson Databind (micronaut-jackson-databind
)
- No,
@Serdeable
is not strictly required. - If your project uses the traditional Jackson integration (
io.micronaut:micronaut-jackson-databind
), Micronaut relies on Jackson's standard behavior. - By default, Jackson can serialize/deserialize most public POJOs and classes without specific Micronaut annotations, similar to how frameworks like Spring Boot operate.
- You can still use standard Jackson annotations (
@JsonProperty
,@JsonIgnore
, etc.) to customize the serialization process. - Note: As of Micronaut Framework 4.0, you must explicitly choose either
micronaut-serde
ormicronaut-jackson-databind
as your serialization implementation. The latter is no longer included transitively by default.
Summary
- If your project relies on
micronaut-serde
for JSON processing (the recommended approach for performance and GraalVM), your API data objects must be annotated with@Serdeable
. - If your project is configured to use
micronaut-jackson-databind
,@Serdeable
is not required for basic serialization, as standard Jackson mechanisms will be used.