I have classes hierarchy in JAVA that should be available through a rest API for an angularJS SPA application (the data persisted in PostgreSQL DB)
the classes hierarchy looks as follows:
- PERSON BASE ABSTRACT CLASS contains name and and age
- CHILD EXTENDS PERSON contains favorite TV show
- PARENT EXTENDS PERSON contains a list of children
- GRANDPARENT EXTENDS PERSON contains a list of of parents
The UI looks like an hierarchy tree, where you can view all the data and edit it.
I could thought of three ways to model this:
- one endpoint with hierarchy /grandparent:id/parents:id/children:id example POST /grandparent/1/parents/2/children in order to create a new child
PROS: more readable, more restful CONS: complex to implement, when I edit a child, I don't care about all the hierarchy at most I need the direct parent in order to create it, but for editing, child ID will be sufficient to find the it in the DB
- three endpoints : /grandparents:id, /parents:id, /children:id POST /children?parentID=2
PROS: ? CONS: a lot of duplication
3.one endpoint /people (in the request/response JSON, use type field to denote the class and a parentID field.
POST /people { name .. type : child parentId : 2 }
PROS: more polymorphic CONS: less restful?
what do you think?
