spring-projects / spring-projects/spring-data-rest
404 on setting an association resource [DATAREST-943]
@odrotbohm is already working on this.
Since Dec 31, 2020.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
Bert Roos opened DATAREST-943 and commented
My first experiment with Spring Data Rest fails. I've taken the "Getting Started" and modified it a bit to create two entities that have a many-to-one relationships. The entities are Book and Shelve and many books can share a shelve.
The Shelve entity looks like this:
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;
}
}
The Book refers to it:
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;
}
}
The full project is available here on GitHub.
Now I can add a 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"
}
}
}
Then a book:
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"
}
}
}
Then I try to put the book on the shelve, but that fails:
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"}
I posted this earlier here on StackOverflow. As I suspect an issue in Spring Data Rest, I am posting it here as defect.
Your help is greatly appreciated.
Affects: 2.5.5 (Hopper SR5)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.