user3364192
user3364192

Reputation: 3933

Java Spring Rest API. Returning referenced objects or object's ids?

I am implementing simple social media api. Each Post object has a field userId - identifier which can be used to pull User object (author of the post) from database.

When client application asks for some post should I give back Post object as is and provide a way to retrieve User for and given userid or prepare an object that already contains full user object?

variant 1:

{"content":"cool post",userId:1}

variant 2:

{"content":"cool post","user": {"name":"Mike"}}

Upvotes: 0

Views: 129

Answers (1)

ketan vijayvargiya
ketan vijayvargiya

Reputation: 5659

It depends on, for example, how you expect your clients to use the data. Are most clients going to use all User details, or will most clients be interested in only the content field?

If you want to provide more flexibility to the clients, consider adding a field, say isFullUserInfoNeeded, in request params. Depending on whether it is set to true or not, you can respond with full User details or just the userId.

Upvotes: 1

Related Questions