I am designing an API using Spring boot for some social network backend. My current model looks like this:
public class User {
private long id;
private String handle;
private String name;
private List<User> followers;
private List<User> following;
// Getters setters etc
Now, I have created DTO which closely resembles above structure. My problem is that sometimes I want to return exactly what's above (which is fine) but sometimes, I don't want that.
For example when someone is only interested in finding followers of user, I don't want to include followers and following (I am simply interested in id
, handle
and name
so computing followers
and following
for all of those users would be an incredible waste of resources).
In my current implementation those fields are returned with null
values, which I don't think is a great idea. Should I create a separate DTO without those lists with just id
, handle
and name
? Or is there more elegant way to do it?