Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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"
        ...
    },
    ...
]
share|improve this question
    
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 '13 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. –  alejandromp May 22 '13 at 20:35
    
Do companies and users have any relation? Could this be as simple as something like /company/(:x)/user/(:x)? –  Jacob Krustchinsky May 22 '13 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 '13 at 21:56
    
I updated my question –  alejandromp May 23 '13 at 7:14

3 Answers 3

up vote 0 down vote accepted

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).

share|improve this answer

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, HAL. Have a look at http://stateless.co/hal_specification.html

share|improve this answer

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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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