Make "data" transient or serializable SonarQube Error

Viewed 1454

I have created this class

@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(of = { "id" })
@Builder(toBuilder = true)
@JsonInclude(NON_NULL)
public class Hostel<T> implements Serializable {

    @Field
    private Long version;   
    @JsonProperty("_data")
    private @NotNull T data;

}

But I have this SonarQube error:

Make "data" transient or serializable SonarQube Error

but data is a Generic type

2 Answers

You can make it transient, or just bound T with <T extends Serializable>.

T is unknown type here. So, it's unpredictable whether T is serializable or not. most of the inbuilt classes of java are serializable e.g. String, Map, List etc but when you use generic types it's unpredictable and hence bound it to be Serializable Or if you don't want it to be serialized then mark it transient.

Read here for more on bounded types

Related