You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 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 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.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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 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 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.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions