Posting a @Onetomany Sub-Resource Association in Spring Data Rest

POSTing a @OneToMany sub-resource association in Spring Data REST

You have to post the comment first and while posting the comment you can create an association posts entity.

It should look something like below :

http://{server:port}/comment METHOD:POST

{"author":"abc","content":"PQROHSFHFSHOFSHOSF", "post":"http://{server:port}/post/1"}

and it will work perfectly fine.

How to manage a one to many entity using JPA and Spring Data Rest

I answered a similar question - so you might want to look at this one. I tried to explain the different use cases there in detail.

https://stackoverflow.com/a/34864254/5371736

Does that apply to your usecase

Spring Data-Rest POST to sub-resource

If you want to associate a Task with a Person you need the link to the person.

Lets say the person URI is http://localhost/persons/1

Then you could assign the person to a task by just passing that URI in the person attribute.

So a post to Task could look like this:

{
"description": "some text",
"person": "http://localhost/persons/1"
}

Spring-data-rest will lookup the person and take care of the rest.

Can't save a OneToMany/ManyToOne relationship in Spring Data REST request

The problem can be solved by:

  1. Add a new repository ParticipationRepository (extends JpaRepository);
  2. Create first a Contract without Participations:

POST http://localhost:8080/api/contracts { "name": "Contract1" }

Response:

201 Created
{
"name": "Contract1",
"_links": {
"self": {
"href": "http://localhost:8080/api/contracts/3"
},
"contract": {
"href": "http://localhost:8080/api/contracts/3"
},
"participants": {
"href": "http://localhost:8080/api/contracts/3/participants"
}
}
}

  1. Create a Participation and use the URI from the just created Contract to set the FK. Assume Contact 1 already exists in the Data Base.

POST http://localhost:8080/api/participations {
"clauses": "bla, bla, bla",
"contract": "http://localhost:8080/api/contracts/3",
"contact": "http://localhost:8080/api/contacts/1" }

Response:

201 Created
{
"clauses": "bla, bla, bla",
"_links": {
"self": {
"href": "http://localhost:8080/api/participations/5"
},
"participation": {
"href": "http://localhost:8080/api/participations/5"
},
"contract": {
"href": "http://localhost:8080/api/participations/5/contract"
},
"contact": {
"href": "http://localhost:8080/api/contacts/5/contact"
}
}
}

In Spring data rest is possible to add nested @OneToMany object to parent over POST /parent/{parentId}/nested?

There is a a section "The association resource" in the spring data rest documentation that tells you what is possible on this resource. And creating a new child and associating it is not possible.

I do not know the why this is.

The only way (I know of) to get this behaviour is to implement a custom controller that does that for you. (Make sure you do not use the @RequestMapping annotation on class level on your custom controller to not override more spring data rest functionality than you want to)

Spring Data Rest and entities' IDs in relationships

You can create a projection and use it by default with an excerpt.

To define the projection :

@Projection(
name = "customBook",
types = { Book.class })
public interface CustomBook {

@Value("#{target.id}")
long getId();

String getTitle();

@Value("#{target.getLibrary().getId()}")
int getLibraryId();
}

And call :

http://192.168.33.20:8080/books/22?projection=customBook

To use this projection by default configure your repo :

@RepositoryRestResource(excerptProjection = CustomBook.class)
public interface BookRepository extends CrudRepository<Book, Long> {}


Related Topics



Leave a reply



Submit