I have a class Person.
Person {
String firstName;
String lastName;
String Date dob;
String email;
String mobileNumber;
String address;
}
To add a person, I have following REST APIs:
POST /person
{ "firstName":"Donald", "lastName":"Trump", "dob":"01/01/1990" }
PUT /person/{id}/addContact
{ "email":"[email protected]", "mobileNumber":"+1-123-456-789" }
PUT /person/{id}/addAddress
{ "address":"white house" }
Now there are two ways to do that -
Use same Person class and keep adding new information in the same object from API 1, 2 and 3.
Create separate models for all three APIs.
PersonMain { String firstName; String lastName; String Date dob; } PersonContact { String email; String mobileNumber; } PersonAddress { String address; }
Finally, we also need our main Person class because all that information is going into single Person table and finally this whole Person object will be used at every place.
What do you think which approach is good and why?
1 Answer 1
Conceptually, you're probably coupling data model and REST resources too much.
Use same Person class and keep adding new information in the same object from API 1, 2 and 3.
Create separate models for all three APIs.
Regardless of the REST resources, I would choose option 2, not "for all 3 API's" but because Person, Contact and Address are separate business concepts and they deserve their own (Value) Object.
As a side note regarding your URIs, to me, "add something" is almost always going to be a POST
. In other words, 2. and 3. are not REST idiomatic.
-
Thanks @guillaume31 for your answer. Agree on your side note, it was just an example not a real API. Coming back to your points about coupling data model and REST resource. Can't we work on understanding requirements of our UI and app team? What kind of APIs they need and then model our things which can serve their purpose?Prashant Bhardwaj– Prashant Bhardwaj2019年06月02日 10:08:33 +00:00Commented Jun 2, 2019 at 10:08
Explore related questions
See similar questions with these tags.
PUT /person/{id}/addAddress
on an existing Person object the only way to set the address, or can it also be set as part of a complete Person object withPOST /person/
orPUT /person/{id}
?