REST API和多态 [英] REST API and polymorphism

查看:190
本文介绍了REST API和多态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JAVA中有类层次结构,应该通过angularJS SPA应用程序的rest API(PostgreSQL DB中保存的数据)提供

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摘要CLASS包含姓名和年龄

  • CHILD EXTENDS PERSON包含收藏的电视show

  • 家长扩展PERSON包含孩子列表

  • GRANDPARENT EXTENDS PERSON包含父母列表

  • 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 parents

UI看起来像一个层次结构树,您可以在其中查看所有数据并进行编辑。

The UI looks like an hierarchy tree, where you can view all the data and edit it.


  1. 一个带有层次结构/祖父母的端点:id / parents:id / children:id

    example

    POST / grandparent / 1 / parents / 2 / children

    为了创建一个新孩子

PROS:更具可读性,更加宁静
缺点:复杂的实现,当我编辑一个孩子时,我并不关心所有层次结构我最需要的是直接父级才能创建它,
但是对于编辑,子ID就足以找到它在数据库中

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


  1. 三个端点:/祖父母:id,/ parents:id,/ children:id

    POST / children?parentID = 2

PROS:
缺点:大量重复

3.一个端点/人(在请求/响应JSON中,使用类型字段到表示类和parentID字段。

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
}

POST /people { name .. type : child parentId : 2 }

PROS:
more polymorphic
缺点:不太安静?

你怎么看?

推荐答案

我认为你应该只有一个'人'端点和一个'人'端点。

I think you should just have a 'people' endpoint, and a 'person' endpoint.

人员端点会返回一个列表的person_ids例如'has_children'= true将返回所有父母.'has_parents'= false将返回所有试管婴儿等等。

The people endpoint would return a list of person_ids fitting a criteria. For instance 'has_children' = true would return all parents. 'has_parents' = false would return all test tube babies, etc etc etc.

'人'端点给出一个特定的id会返回有关特定人的详细信息。在这里你会看到眼睛的颜色,性别,一个孩子的人名列表,一个pa列表租用person_ids,也许是孙子person_id的列表。关于此人的任何其他详细信息都将在此处。

The 'person' endpoint would return details about the specific person, given a specific id. Here you would see eye color, gender, a list of children person_ids, a list of parent person_ids, perhaps a list of grandchildrens person_id. Any other details about the person would be here.

如果扩展类没有添加任何重要功能,您可能需要考虑重构Java代码简单地从一个人类中获得。

You might want to consider refactoring the Java code if the extended classes don't add any significant functionality that you couldn't get simply from a person class.

基本上你在这里处理人的'节点'。

Essentially you are dealing with person 'nodes' here.

退房Graph Commons API可能会为您完成大部分工作。

Check out Graph Commons API maybe much of your work is done for you.

这篇关于REST API和多态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆