Issue
Currently I am trying to authenticate an User in my system that resides in the user-service and I have two other microservices, authentication-service (is basically my gateway used for authentication) and delivery-service (used to just create a delivery for an user). Both of these services expect to receive an UserDTO which is represents an User entity differently.
UserDTO for authentication-service:
public class UserDTO {
private String email;
private String password;
// constructor, getters, setters
}
UserDTO for delivery-service:
public class UserDTO {
private String firstName;
private String lastName;
private String homeAddress;
private String contactNumber;
// constructor, getters, setters
}
According to https://www.baeldung.com/java-microservices-share-dto I find it a good idea to use Client Modules to share these so I created the following modules for my user-service:
user-service
|__ user-client
|__ user-server
user-service
└──user-client
UserClient.java
UserClientImpl.java
UserAuthenticationResponseDTO.java --- This contains the email and password fields requiered by authentication-service
UserOrderResponseDTO.java --- This contains the fields required by the order-service
My question is, do you think this is a good solution to my issue ? I think this will also solve the issue when I will start making requests as well because for example to make a request to the delivery-service the homeAddress field should be required but I don't want to have a required homeAddress field when I make a request to my authentication-service
Solution
This is more of a personal opinion than anything, but each element in the business unit should represent something different than other elements, and serve a single purpose. So, if the Authentication and Delivery business elements serve different purposes (even if they refer to the same database object, for instance) I don't see any problem with your approach.
For your last sentence, there are filters and interceptors to validate objects in requests previous to any interaction, or even in the service itself, or the DTO object. So unless you are using some sort of unmodifiable field filter and validator object, you shouldn't run into required field issues unless you explicitly code it.
Answered By - Mauricio
Answer Checked By - Timothy Miller (JavaFixing Admin)