Why do we have to copy-paste the same CRUD controller for every entity? Why do we have to write the same linking REST controllers for @OneToMany, @ManyToMany or @OneToOne relationships? With spring-rest-data you don’t have to do it anymore.
Table of contents
- Spring Data REST
- Object creation
- Object linking
- Link removal
1. Spring Data REST
When you want to develop quick and straightforward REST API for some entities (with relations), you should check Spring-data-rest module. It’s built on top of Spring-data module which makes hypermedia-driven REST web services on top of Spring data repositories. It uses HAL as media type which makes consistent and easy way to hyperlink between all objects. If you are not familiar with this format, you can check Spring HATEOAS.
Let’s consider a spring-boot project which is so easy to do.
Please note that for shortage purpose we used project Lombok for @Getter and @Setter. If you are not familiar with it, please check this out!
Spring boot main class
It will be used to say spring-boot that we want to setup Hibernate with H2.
Is the @RestResource required annotation? No! If you don’t need anything more special that ‘/pet/owner’ endpoint, you do not have to write it. It may be used for customising the endpoint.
What did we do? One project, two model classes, one relation, two interfaces without any implementation. You may ask what more? That’s it and it works. 🙂 You may ask what we have? Full CRUD REST API for Owner and Pet entity with linking API.
3. Object creation
We did not preload database so let’s create a Pet.
4. Object linking
So let’s add pet to owner.
And check if the association was persisted.
From Pet’s side, let’s retrieve an owner/
And from Owner’s side, we can retrieve pets.
5. Link removal
We may also remove an association with:
But what if we will have a simple application with many records? We may need some pagination. We may use javax.persistence.Query with bound pagination or Criteria API but it’s just a boilerplate code which repeats and repeats. With Spring-data-rest what we only need is… interface.
Let’s change our PetRepository.
In the meantime let’s load some records into the database to check the pagination.
Let’s consider validation using org.springframework.validation.Validator. To make it fully work, we need to: implement validator, register it on Spring Data REST event.
There are eight different data REST events:
Resource bundle (messages.properties)
Validator registration on “beforeCreate” event
As you can see, you can save your time by learning Spring Data REST. The code is clean and readable.
For above code please check GitHub.