简体   繁体   English

Spring Data Rest POST请求

[英]Spring data rest POST request

I'm working on a quiz system using Spring Data Rest that has the following structure: 我正在使用具有以下结构的Spring Data Rest开发测验系统:

1 Quiz contains many Questions 1个测验包含许多问题
1 Question contains many Answers 1个问题包含许多答案

We want to represent this with the following URL structure: 我们想用以下URL结构来表示:

GET /quiz-service/quizzes/1/questions/1/answers - Gets all the answers belonging to question 1 within quiz 1. GET / quiz-service / quizzes / 1 / questions / 1 / answers-获取测验1中属于问题1的所有答案。
POST /quiz-service/quizzes/1/questions/1/answers - Adds an answer to question 1 within quiz 1. POST / quiz-service / quizzes / 1 / questions / 1 / answers-在测验1中为问题1添加答案。
POST /quiz-service/quizzes/1/questions - Adds a question to quiz 1. POST / quiz-service / quizzes / 1 / questions-向测验1添加问题。

At the moment, when we try and POST to the above, we get an HTTP 40-something code. 目前,当我们尝试将其发布到上面时,我们得到了HTTP 40左右的代码。

All IDs in the above URLs are unique. 上述网址中的所有ID都是唯一的。 The following, with 2 different quizzes referring to the same question, would cause a problem: 以下内容,使用2个不同的测验来引用相同的问题,将引起问题:

/quiz-service/quizzes/1/questions/1 - would be HTTP OK / quiz-service / quizzes / 1 / questions / 1-是HTTP OK
/quiz-service/quizzes/2/questions/1 - would result with HTTP NOT FOUND given the above. / quiz-service / quizzes / 2 / questions / 1-鉴于上述情况,HTTP NOT FOUND可能导致结果。

We have a repository interface for all 3, quizzes, questions and answers. 我们为所有3个测验,问题和答案提供一个存储库界面。

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. 根据这篇文章以及我们所见过的其他文章 ,我们知道您可以发布到/ quiz-service / questions并添加到测验的链接,或者向测验端点发出第二个请求,以添加问题。 Despite this, is there any way at all that we can do the above? 尽管如此,我们有什么办法可以做到以上几点?

Thanks in advance 提前致谢

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. Spring Data REST大量利用了超媒体,因此,尽管您可以对URI结构进行一定的控制,但无法(或者应该有需要)将其调整为您希望的样子。 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. 随着超媒体的使用,设计URI方案基本上变得无关紧要。 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. 重要的是资源公开的表示形式,资源包含的链接等。SpringData REST为您默认了很多设置,所有这些都在上面链接的文档中进行了描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM