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 am working on a project and would like to expose some rest services. The technologies used are RestEasy, EJB and JPA/Hibernate. I have structured my app so that there is a clear delimitation between the service layer and model layer. A typical service that retrieves data from the database have a signature like this: getWhateverList(idForWhichTheListWillBeRetrieved, start) - so pretty basic.

What I'm having a hard time wrapping my head around is how to bring together the stiffness of the service layer with the flexibility of the rest layer. Say for example that I would like a typical GET method producing a json array support operations like:

  • filtering - use a query param to get, for example items that have a property of type date greater/lower than the param that I provide
  • sorting - rest api should support asc or desc
  • limiting the fields that are returned from the GET operation
How would one go about designing this?

share|improve this question
    
if you're using Spring you should check out Spring Data REST. There are some decisions made for you in that space, around the GET,PUT, DEL .. etc with some nice examples. –  Erik Nov 22 '13 at 16:28
    
sorry for the incredible late response, had a busy period. i did look into spring's solution and it provided some useful pointers for me to integrate in my application. @Erik thanks for the hint –  Radu Jan 7 at 12:46

1 Answer 1

  • filtering - use a query param to get, for example items that have a property of type date greater/lower than the param that I provide

For that you need a query language. You can use any existing query language as long as you describe it with metadata in your links. So your clients and server can have a common contract which is described by that metadata. For example GET /users?q="statement using a standard query language".

You can describe simple queries using URI templates. For example GET /users?firstName={firstName}

  • sorting - rest api should support asc or desc

You can use the same links described above with additional query params. For example GET /users?sortBy="name"&direction="asc".

  • limiting the fields that are returned from the GET operation

You can use the same links described above with additional query params. For example GET /users?fields="firstName,secondName,age".

note: According to the uniform interface / HATEOAS constraint you have to send links to your clients. According to the uniform interface / self-descriptive messages constraint, these links have to contain meta-data (for example link relation) about what they are. So the clients can check that meta-data and chose the proper link to reach their goal.

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.