How should I deal with object hierarchies in a RESTful API?

后端 未结 4 854
栀梦
栀梦 2020-12-22 19:57

I am currently designing the API for an existing PHP application, and to this end am investigating REST as a sensible architectural approach.

I believe I have a reas

4条回答
  •  难免孤独
    2020-12-22 20:37

    I would recommend Restful Obects which is standards for exposing domain model's restful

    The idea of Restful Objects is to provide a standard, generic RESTful interface for domain object models, exposing representations of their structure using JSON and enabling interactions with domain object instances using HTTP GET, POST, PUT and DELETE.

    According to the standard, the URIs will be like:

    • api.example.com/object/user/31
    • api.example.com/object/user/31/properties/username
    • api.example.com/object/user/31/collections/channels
    • api.example.com/object/user/31/collections/members
    • api.example.com/object/user/31/actions/someFunction
    • api.example.com/object/user/31/actions/someFunction/invoke

    There are also other resources

    • api.example.com/services
    • api.example.com/domain-types

    The specification defines a few primary representations:

    • object (which represents any domain object or service)
    • list (of links to other objects)
    • property
    • collection
    • action
    • action result (typically containing either an object or a list, or just feedback messages)
    • and also a small number of secondary representations such as home, and user

    This is interesting as you’ll see that representations are fully self-describing, opening up the possibility of generic viewers to be implemented if required.

    Alternatively, the representations can be consumed directly by a bespoke application.

提交回复
热议问题