-
Hi there! I'm using Java records for my DTOs, which happen to have fields of type List/Map, and I'm trying to enforce immutability. My current DTO looks like: import com.google.common.collect.ImmutableList;
public record MyDTO(
@JsonProperty("id") String uuid,
@JsonProperty("items") @JsonDeserialize(as = ImmutableList.class) List<String> items){
} But it throws: io.micronaut.core.beans.exceptions.IntrospectionException: No bean introspection available for type [class com.google.common.collect.ImmutableList]. Ensure the class is annotated with io.micronaut.core.annotation.Introspected. The workaround that I could use is using compact constructors to make the collection immutable, like this: public record MyDTO(
@JsonProperty("id") String uuid,
@JsonProperty("items") List<String> items){
public MyDTO {
items = items == null ? List.of() ? items.stream()
.filter(Objects::nonNull)
.toList();
}
} But I wonder if there is a better approach to achieve immutability, ideally an annotation or an annotation-parameter that doesn't rely on third-party dependencies. I checked the documentation on Micronaut Serialization, and I haven't seen any annotation or configuration to enable this feature. Any suggestion is appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
For Micronaut Serialization, you can define custom deserializers/serializers or use |
Beta Was this translation helpful? Give feedback.
-
I ended up solving my issue by annotating my collection fields with either:
|
Beta Was this translation helpful? Give feedback.
I ended up solving my issue by annotating my collection fields with either:
@Serdeable.Deserializable(as = ImmutableList.class)
@Serdeable.Deserializable(as = ImmutableMap.class)