Search code examples
javaspringapidto

dto entity for form or create special dto form entity for form


Lets imagine that I have user entity and some registration form. Should I return userDto or I should return some RegistrationForm entity and map it to domain model.


Solution

  • Normally you don't need a RegistrationForm entity and User entity would be enough to keep user data. On the other hand, you can create two separate dto e.g. SignupRequest for sending form data to the corresponding service and SignupResponse for sending response after registering the user.

    Here is an example usage:

    @Data
    public class SignupRequest {
    
        private String firstName;
        private String lastName;
        private String username;
        private String password;
    }
    

    @Data
    public class SignupResponse {
    
        String message;
        Long userId;
        String username;
        String accessToken;
        String refreshToken;
    }