Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 404关于设置关联资源_Spring_Spring Data Rest - Fatal编程技术网

Spring 404关于设置关联资源

Spring 404关于设置关联资源,spring,spring-data-rest,Spring,Spring Data Rest,我对Spring数据Rest的第一次实验失败了。我采用了“入门”并对其进行了一些修改,以创建具有多对一关系的两个实体。实体是Book和Shelve,许多书籍可以共享一个书架 搁置实体如下所示: package hello; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persisten

我对Spring数据Rest的第一次实验失败了。我采用了“入门”并对其进行了一些修改,以创建具有多对一关系的两个实体。实体是
Book
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;
    }
}
一书
提到了它:

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;
    }
}
完整的项目是可用的

现在我可以添加一个搁置:

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"}

知道这里出了什么问题吗?

您可能需要指定“书架”一侧的链接,然后尝试将书添加到书架

//货架模型

@OneToMany(mappedBy = "shelve")
public Set<Book> getBooks() {
    return books;
}

public void setBooks(Set<Book> books) {
    this.books = books;
}

正如Oliver Gierke在上所指出的,问题在于
curl
请求。它应该在
内容类型中有一个宣传:

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
因此,只要您使用正确的方式,一切都能按预期进行:)


@奥利弗:非常感谢

那可能行得通,但那不是我想要的模型。上面的代码显然只探索Spring数据Rest。“我需要一个ManyToOne。@伯特你添加了@onetomany otherside部分并尝试了ManyToOne添加吗?不,我没有这样做,因为我不想在我的模型中添加它。我需要一个关于关系的单向导航。如果您正在使用org.springframework.web.client.RestTemplate或org.springframework.boot.test.web.client.TestRestTemplate,请确保在创建HttpEntity时没有忘记指定contentType,如下代码所示:HttpHeaders headers=new HttpHeaders()headers.setContentType(MediaType.valueOf('text/uri list'))HttpEntity实体=新的HttpEntity(“”+myresource.id,headers)
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