Saving spring related data objects to store HAL-JSON data

I have the following problem

I have a basic spring data breakpoint configuration (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 the object, it works fine, but I don’t know how to save the relationship

eg. save "A" inside "B" using http

This is the last thing that I am trying to answer this question https://stackoverflow.com/a/4646263

POST http://localhost:8080/api/a

{
    "name": "Name of A",
    "b": {
        "rel": "b",
        "href": "http://localhost:8080/api/b/1"
    }
}

I get 201 http code but not save object.

Has anyone tried this already?

+4
source share
1

URL.

POST http://localhost:8080/api/a
Content-Type: application/json

{
    "name" : "Name of A",
    "b": "http://localhost:8080/api/b/1"
}

, , ,

"b" :  ["http://localhost:8080/api/b/1"]

A.b - , , . .

Spring 2.0 (. Spring Data Rest 2.0.0.RELEASE RC1), .

0

Source: https://habr.com/ru/post/1533647/


All Articles