As far as I know, Serializable should be used when deserialization is needed e.g. using dto as return object in APIs. But should I use Serializable for the whole DTO objects or just for the general one?
@Data
@AllArgsConstructor
public class GaDTO implements Serializable {
private static final long serialVersionUID = -xxxx;
private String id;
private GaStatus gaStatus;
private PlaTurn plaTurn;
private PlaDTO pla1;
private PlaDTO pla2;
}
This DTO is the return type of ResponseEntity
but there are other DTOs that this GaDTO uses.
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PlaDTO implements Serializable {
private static final long serialVersionUID = -yyyyyy;
private List<PiDTO> pis;
private PiDTO mainPi;
}
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PiDTO implements Serializable {
private static final long serialVersionUID = -zzzzzz;
private Integer stos;
}
Should I only use Serializable for GaDTO or do other DTOs need it too? And why should I use it?
CodePudding user response:
You don't need to implement Serializable and Deserializable for your DTO because it is already handled by Spring Boot Jackson Package.
CodePudding user response:
In most cases, there is no reason do make DTO Serializable, but I would like to extend my answer by adding this idea that in a more pragmatic architectures, the domain objects could also be exposed directly to the presentation layer. So instead of copying the data from domain objects into DTOs, which violates DRY Principal, you could just pass the detached JPA-entities as value holders to the web application. In this case it is better to implement java.io.Serializable. So there is no need to do that.