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

后端 未结 4 845
栀梦
栀梦 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:39

    The best advice I can give is to try and avoid thinking about your REST api as exposing your objects. The resources you create should support the use cases you need. If necessary you might create resources for all three options:

    api.example.com/completeuser/{id}
    api.example.com/linkeduser/{id}
    api.example.com/lightweightuser/{id}
    

    Obviously my names are a bit goofy, but it really doesn't matter what you call them. The idea is that you use the REST api to present data in the most logical way for the particular usage scenario. If there are multiple scenarios, create multiple resources, if necessary. I like to think of my resources more like UI models rather than business entities.

提交回复
热议问题