REST complex objects
There are two approaches to managing complex objects in REST where we can include the entire object in its operation and/or provide specific paths for each object
Invoice creation as a whole object
POST /invoice
{
"invoiceId": 1000,
"invoiceDate": "02/04/2001",
"vendorName": "XYZ Industries Ltd",
"approving Manager": null,
"accountNumber": null
"address":{
"streetName":"123 St Kilda Road",
"suburb": "St Kilda",
"state":"VIC",
"postcode":3000
}
}
Create the subobjects on their own like below
POST /invoice/1000/address
"address":{
"streetName":"123 St Kilda Road",
"suburb": "St Kilda",
"state":"VIC",
"state":"VIC",
"postcode":3000
}
Managing the state in REST can be quite challenging. The change in state may trigger different validation and business rules used to process that payload. Rather than using complex logic we could use the resource paths to model the representation of each state.
Example
Modifying the pre-approval and post-approval of a workflow item. Below is an invoice that requires approval for payment.
Invoice creation state
POST /invoice
{
"invoiceId": 1000,
"invoiceDate": "02/04/2001",
"vendorName": "XYZ Industries Ltd",
"approving Manager": null,
"accountNumber": null
}
Invoice approval state
PUT /invoice/1000/approval
{
"invoiceDate": "02/04/2001",
"vendorName": "XYZ Industries Ltd",
"approving Manager": "Tom",
"accountNumber": null
}
Invoice payment state
PUT /invoice/1000/payment
{
"invoiceDate": "02/04/2001",
"vendorName": "XYZ Industries Ltd",
"approving Manager": "Tom",
"accountNumber": 6597465923056
}
No comments:
Post a Comment