1

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":"[email protected]",
    "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?

4
  • Trump is in his late 30s? Fake news. Commented Jan 4, 2019 at 2:13
  • User name checks out. Commented Jan 4, 2019 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}? Commented Jan 4, 2019 at 9:09
  • Yes @BartvanIngenSchenau it can also be set with POST /person/ or PUT /person/{id}. Commented Jun 2, 2019 at 10:04

1 Answer 1

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.

1
  • 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? Commented Jun 2, 2019 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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