0

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:

  1. POST /person

    {
    "firstName":"Donald",
    "lastName":"Trump",
    "dob":"01/01/1990"
    }
    
  2. PUT /person/{id}/addContact

    {
    "email":"donald.trump@us.com",
    "mobileNumber":"+1-123-456-789"
    }
    
  3. PUT /person/{id}/addAddress

    {
    "address":"white house"
    }
    

Now there are two ways to do that -

  1. Use same Person class and keep adding new information in the same object from API 1, 2 and 3.

  2. 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?

  • Trump is in his late 30s? Fake news. – candied_orange Jan 4 at 2:13
  • User name checks out. – VoiceOfUnreason Jan 4 at 2:15
  • 1
    Is using 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 with POST /person/ or PUT /person/{id}? – Bart van Ingen Schenau Jan 4 at 9:09
  • Yes @BartvanIngenSchenau it can also be set with POST /person/ or PUT /person/{id}. – Prashant Bhardwaj Jun 2 at 10:04
1

Conceptually, you're probably coupling data model and REST resources too much.

  1. Use same Person class and keep adding new information in the same object from API 1, 2 and 3.

  2. 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 Jun 2 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.