1

I'm working on a quiz system using Spring Data Rest that has the following structure:

1 Quiz contains many Questions
1 Question contains many Answers

We want to represent this with the following URL structure:

GET /quiz-service/quizzes/1/questions/1/answers - Gets all the answers belonging to question 1 within quiz 1.
POST /quiz-service/quizzes/1/questions/1/answers - Adds an answer to question 1 within quiz 1.
POST /quiz-service/quizzes/1/questions - Adds a question to quiz 1.

At the moment, when we try and POST to the above, we get an HTTP 40-something code.

All IDs in the above URLs are unique. The following, with 2 different quizzes referring to the same question, would cause a problem:

/quiz-service/quizzes/1/questions/1 - would be HTTP OK
/quiz-service/quizzes/2/questions/1 - would result with HTTP NOT FOUND given the above.

We have a repository interface for all 3, quizzes, questions and answers.

As per this post and others we've seen, we know that you can POST to /quiz-service/questions and either include a link to the quiz, or make a 2nd request to the quiz endpoint adding the question. Despite this, is there any way at all that we can do the above?

Thanks in advance

Community
  • 1
  • 1
  • What's the specific error you're getting? – Makoto Jul 30 '15 at 14:30
  • Well my advice would be to use more endpoints, not because what you propose cannot necessarely be made but its gonna be very confusing on the long term. Try to follow REST patterns and set an endpoint for each i.e. one for adding answers and one for adding questions etc. – breakline Jul 30 '15 at 14:31
  • You can use this as a guide to design the rest APIs more intuitive. Especially the section "Use RESTful URLs and actions" http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api – Tito Jul 30 '15 at 19:47

1 Answers1

1

Spring Data REST leverages hypermedia intensively, so while you have certain control over the URI structure, there's no way (or should there be a need) you tweak it to the way you want it to look like. The pragmatic way here is to leverage the link relations that are exposed and use them as documented in the reference documentation.

With the use of hypermedia, designing a URI scheme basically becomes irrelevant. What matters are the representations a resource exposes, which links it contains etc. Spring Data REST defaults a lot of that for you, all of this described in the documentation linked to above.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211