What is the proper format to expect when using an api to create a resource that references another resource

Viewed 19

I am adding a feature that allows users to select from a list of people of a certain type, Type1 and Type2. A type would be chosen from a dropdown, and the data from the API would look like

{
    "id": 1,
    "name": "TYPE1",
    "desc": "Type 1 Person"
}

I am creating a POST endpoint that allows an admin user to insert more people into the list, but I'm unsure on the best way for the admin to include the person's type. In other languages/frameworks, I would do something like this:

{
    "first_name": "John",
    "last_name": "Doe",
    "type_id": 1
}

then handle adding the entry in my own SQL. In Spring though, I'm trying to leverage an object being created from the data automatically. For this to be successful, I've need to send the data as:

{
    "first_name": "John",
    "last_name": "Doe",
    "type": {
        "id": 1,
        "name": "TYPE1",
        "desc": "Type 1 Person"
    }
}

My question is in two parts.

In Spring, is there anything I can leverage that would allow me to just pass an identifier for person type when creating a new person entry? (I've looked into DTOs, but I've never used them, so I don't know if that is the proper solution)

In REST in general, how much data should be required when adding a resource that references another resource?

0 Answers
Related