I use multiple DTOs for a same Entity.
For example, my entity:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "users")
public class User {
@Id //(with uuid generator)
private UUID id;
private String firstName;
private String lastName;
private String password;
private String phoneNumber;
private Attachment photo;
}
The problem is that request and response are not the same. For example, to register, a user must enter a firstName, lastName, password, and phoneNumber. but not the id and photo (file). I wrote another API that works with attachments
I return the user as follows:
{
"id": "c37f5b13-0698-41c8-a439-212484935567",
"firstName": "John",
"lastName": "Doe",
"phoneNumber": "123456789",
"photoUrl": "/api/attachment/3ac27460-1c60-11ec-9621-0242ac130002"
}
and my DTOs:
public class UserDto {
@Data
public static class SignUpParams {
private String firstName;
private String lastName;
private String password;
private String phoneNumber;
}
@Data
public static class SignInParams {
private String password;
private String phoneNumber;
}
@Data
public static class ResponseParams {
private UUID id;
private String firstName;
private String lastName;
private String phoneNumber;
private String photoUrl;
}
@Data
public static class BlaParams {
// Just the fields I want
}
}
I think this method does not return unnecessary null values and is not displayed in Swagger Api Docs.
Is it right and best practices to use a nested class for dtos? or is there another better option?