11

I have a REST webservice that have a couple of resources, like Company and User. But now I need to make a modification to the API to return a special list combining company, user and maybe other type of resource. Like a list of recommendations with various type of resources.

Which is the best approach to solve this?

I need to make a new endpoint (resource), something like: /recommendations? I already have the /company endpoint, and the /user endpoint returning the corresponding objects. This is fine. But now I need some way to return an array with a combination of users and companies.

Also I'm thinking that the json will look like an array of objects, but how the client can know which type of object is parsing if every object in the array is a different resource?

EDIT

I want that my client could show a recomendations screen in the app. The problem is that this list can combine multiple types of data (users, companies). It doesn't matter if this resources have a relationship or not. The client shoud recive a json like that:

"recomendations": [
    {
        "id_user": 1
        "name": "Jean"
        ...
    },
    {
        "id_company": 2
        "name": "Starbucks"
        ...
    },
    ...
]
5
  • Will you end up with 3 endpoints: /Company, /User, /Recommendations ? And you're returning an array of serialized objects correct (i.e. some conforming structure)? Company should return a Company Structure, User a User Structure, and Recommendations a "Recommendations Structure"? other than a complex object, i'm not sure what your asking
    – hanzolo
    May 22, 2013 at 19:51
  • I alredy have the /company endpoint, and the /user endpoint returning the corresponding objects. this is fine. but now I need some way to return an array with a combination of users and companies. May 22, 2013 at 20:35
  • Do companies and users have any relation? Could this be as simple as something like /company/(:x)/user/(:x)? May 22, 2013 at 21:35
  • 1
    What type of idea is Recommendations supposed to be expressing? It seems that there must be a structured result set like: Recommendations.users and Recommendations.companies for example. If you could elaborate on "recommendations" that will be helpful
    – hanzolo
    May 22, 2013 at 21:56
  • I updated my question May 23, 2013 at 7:14

3 Answers 3

7

Map your Recommendation resources to /recommendations and then use this structure:

{
   "users" : [ 
           { .. }, 
           { .. }, ..
    ],
   "companies" : [
           { .. }, 
           { .. }, ..
    ]
}

It allows the consumer to parse it properly based on the name (users or companies for instance).

0
5

I would define a separate Recommendation type with all the relevant information for recommendations in it. In your model, this type does not map directly to a database table, but rather gets its contents from both the User and Company tables (possibly using an SQL UNION).

2

Without making assumptions about relationships between Company and User, I think you just want a nice way to return composition of different type of objects. There's a proposed standard that pretty well defines how to do this, Hypertext Application Language. Here is an example:

GET /recommendations HTTP/1.1
Host: example.org
Accept: application/hal+json

HTTP/1.1 200 OK
Content-Type: application/hal+json

{
    "_links": {
    "self": { "href": "/recommendations" },
    "next": { "href": "/recommendations?page=2" },
    "find": { "href": "/recommendations{?id}", "templated": true }
},
"_embedded": {
    "recommendations": [{
        "_links": {
            "self": { "href": "/recommendations/123" },
            "users": { "href": "/users/98712" },
            "companies": { "href": "/companies/7809" }
       },
     },{
         "_links": {
             "self": { "href": "/recommendations/124" },
             "users": { "href": "/users/97213" },
             "companies": { "href": "/companies/12369" }
         },
    }]
},
}

Copied from ExampleDocument

2
  • This answer is incomplete. It should either point to section(s) in HAL spec/draft which correspond to the suggested solution or it should present a complete HAL-based example.
    – mloskot
    Dec 8, 2016 at 20:07
  • 1
    @mloskot I updated the link, the old link has lots of stuff removed. The new one now points to the official draft. Hope that helps
    – imel96
    Dec 16, 2016 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.