我的第一个Spring Data Rest实验失败了。我已经采取了#34;入门"并修改了一下以创建两个具有多对一关系的实体。实体是Book
和Shelve
,很多书都可以共享一个货架。
Shelve
实体如下所示:
package hello;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Shelve
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private int length;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
}
Book
引用它:
package hello;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Book
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String title;
@ManyToOne
private Shelve shelve;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Shelve getShelve() {
return shelve;
}
public void setShelve(Shelve shelve) {
this.shelve = shelve;
}
}
完整项目可用here in GitHub。
现在我可以添加一个搁架:
curl -i -X POST -H "Content-Type:application/json" -d "{ \"length\" : \"30\" }" http://localhost:8080/shelves
HTTP/1.1 201
Location: http://localhost:8080/shelves/1
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 17 Nov 2016 16:05:02 GMT
{
"length" : 30,
"_links" : {
"self" : {
"href" : "http://localhost:8080/shelves/1"
},
"shelve" : {
"href" : "http://localhost:8080/shelves/1"
}
}
}
然后一本书:
curl -i -X POST -H "Content-Type:application/json" -d "{ \"title\" : \"The Battle of Life\" }" http://localhost:8080/books
HTTP/1.1 201
Location: http://localhost:8080/books/1
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 17 Nov 2016 16:05:02 GMT
{
"title" : "The Battle of Life",
"_links" : {
"self" : {
"href" : "http://localhost:8080/books/1"
},
"book" : {
"href" : "http://localhost:8080/books/1"
},
"shelve" : {
"href" : "http://localhost:8080/books/1/shelve"
}
}
}
然后我试着把书放在货架上,但那失败了:
curl -i -X PUT -H "ContentType: text/uri-list" -d "http://localhost:8080/shelves/1" http://localhost:8080/books/1/shelve
HTTP/1.1 404
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 17 Nov 2016 16:05:02 GMT
{"timestamp":1479398702523,"status":404,"error":"Not Found","message":"No message available","path":"/books/1/shelve"}
知道这里出了什么问题吗?
答案 0 :(得分:1)
正如Oliver Gierke在the related JIRA ticket所指出的,问题出在curl
请求中。它应该在Content-Type
中有一个大肆宣传:
curl -i -X PUT -H "Content-Type: text/uri-list" -d "http://localhost:8080/shelves/1" http://localhost:8080/books/1/shelve
HTTP/1.1 204
Date: Sat, 19 Nov 2016 16:50:24 GMT
所以一切都按预期工作,只要你以正确的方式使用它:)
@Oliver:非常感谢!
答案 1 :(得分:0)
可能您需要在“货架”一侧指定链接,并尝试将该书添加到货架
//搁置模型
@OneToMany(mappedBy = "shelve")
public Set<Book> getBooks() {
return books;
}
public void setBooks(Set<Book> books) {
this.books = books;
}
// curl将书添加到搁置
curl -i -X PUT -H "ContentType: text/uri-list" -d "http://localhost:8080/books/1" http://localhost:8080/shelve/1/books