Reputation: 873
I have the follow problem
I have a basic configuration of spring data rest (Nothing fancy, nothing custom).
Using spring-data-rest-webmvc 2.0.0 RELEASE and spring-data-jpa 1.5.0 RELEASE
Class A
@Entity
public class A {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id
private String name;
@ManyToMany
private List<B> b;
// getters setters
}
Class B
@Entity
public class B {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id
private String nameb;
@ManyToMany(mappedBy = "b")
private List<A> a;
// getters setters
}
Repository A
@Repository
@RestResource(rel = "a", path = "a")
public interface ARepository extends PagingAndSortingRepository<A, Integer> {
}
Repository B
@Repository
@RestResource(rel = "b", path = "b")
public interface BRepository extends PagingAndSortingRepository<B, Integer> {
}
When I save an entity works fine, but I don't know how to save a relationship
e.g. save an "A" inside a "B" using http
This is the last thing I try from this answer https://stackoverflow.com/a/13031580/651948
POST http://localhost:8080/api/a
{
"name": "Name of A",
"b": {
"rel": "b",
"href": "http://localhost:8080/api/b/1"
}
}
I get an 201 http code but doesn't save the entity.
Did someone tried this already?
Upvotes: 4
Views: 1421
Reputation: 293
Try just using the URL.
POST http://localhost:8080/api/a
Content-Type: application/json
{
"name" : "Name of A",
"b": "http://localhost:8080/api/b/1"
}
or, in your case, it's probably
"b" : ["http://localhost:8080/api/b/1"]
because A.b is a list and hence you submit an array. Did not test this, though.
This should be the valid way since Spring 2.0 (see Spring Data Rest 2.0.0.RELEASE Breaks Code Working Previously With RC1) and it works for me well.
Upvotes: 1