How to pass the model from the server to the client if they have different client and server?

All kind time of day!

Write messenger as a graduation work on Java EE.

There is the ChatRoom model
public class ChatRoom {

 private long id;
 private String name;
 private Map<String, Session> clients;
 private List<User> members;
 MessageEncoder encoder;
 OnlineUsersStorage onlineUsersStorage = OnlineUsersStorage.getInstance();

 public ChatRoom(long id, String name, List<User> members) {
 clients = new HashMap<>();
 encoder = new MessageEncoder();
 this.id = id;
 this.name = name;
 this.members = members;
}

//Setters
 public void setId(long id) {
 this.id = id;
}

 public void setName(String name) {
 this.name = name;
}

 public void setMembers(List<User> members) {
 this.members = members;
}

 public void setClients(Map<String, Session> clients) {
 this.clients = clients;
}


//Getters
 public long getId() {
 return id;
}

 public String getName() {
 return name;
}

 public List<User> getMembers() {
 return members;
}

 public Map<String, Session> getClients() {
 return clients;
}


//Methods
 public void sendMessage(Message message){

 try {
 String encodedMessage = encoder.encode(message);
 List<Session> sessions = onlineUsersStorage.getUserSession(members);
 for (Session s : sessions) s.getBasicRemote().sendText(encodedMessage);

 }catch (IOException | EncodeException e){
e.printStackTrace();
}

}

}


And on the client there is also a ChatRoom

public class ChatRoom {

 private long id;
 private String name;
 private List<User> members;
 private List<Message> messages;

 public ChatRoom(long id, String name, List<User> members, List<Message> messages) {
 this.id = id;
 this.name = name;
 this.members = members;
 this.messages = messages;
}

//Getters
 public long getId() {
 return id;
}

 public String getName() {
 return name;
}

 public List<User> getMembers() {
 return members;
}

 public List<Message> getMessages() {
 return messages;
}

//Setters
 public void setId(long id) {
 this.id = id;
}

 public void setName(String name) {
 this.name = name;
}

 public void setMembers(List<User> members) {
 this.members = members;
}

 public void setMessages(List<Message> messages) {
 this.messages = messages;
}

//Methods
 public Message getLastMessage(){
 return messages.isEmpty() ? null : messages.get(messages.size() - 1);
}
}


The trouble is that they have different implementation.

The client don't need no fields of type
Map<String, Session> clients;
 MessageEncoder encoder;
OnlineUsersStorage onlineUsersStorage


And the server does not need to be inside the ChatRoom
List<Message> messages;

The client is in the process of the program sends a request to server with request to grant him a ArrayList with objects ChatRooms client.

On the server you can implement it? To produce two kinds of models ChatRoom Ala ClientChatRoom/ServerChatRoom on the server seems like a foolish decision.
April 7th 20 at 15:33
2 answers
April 7th 20 at 15:35
Solution
internal objects server and client are not in any way related, even if identical on the fields
customer take special DTO objects (data transfer) and this is not the same as entity models the client or server
And as the client needs them to handle? The client arrives JSON inside the arrayList. What to do with it? I have a client class only ChatRoom - Celine.Kassul commented on April 7th 20 at 15:38
April 7th 20 at 15:37
To use an intermediate model. To generate local objects.

Find more questions by tags AndroidJavaJava EE