Spring Rest Repositories - @RepositoryRestResource

If you are so lazy to write lot of boilerplate code to create simple rest end points to your repository entity, the good news for you. Using Spring Boot and Spring Data Rest, we can develop a fully restful endpoints in a few steps with less code.

Say you have an entity named Product, you will be creating a repository interface which extends JpaRepsitory. Your interface will be like this. We automatically get a bunch of database repository out of box methods like findAll, findOne, etc..
public interface ProductRepository extends JpaRepository<Product, Long>
Previously, we used to create a Controller and create end points for the entity, to support the repository methods. But now the magic happens here in the repository interface.  Just by adding the @RepositoryRestResource annotation at the top of the interface provides us all the rest endpoints. By default, spring will use the pluralized entity name for the endpoint.  Now just start the application and go to http://localhost:8080/products, you see the list of products in your system. It just not end here, you can try all your HTTP methods like DELETE, POST, PUT etc..

Just an entity class and repository interface, you got your rest end points up and running effortlessly.


Happy Programming

Comments

Popular posts from this blog

Method Reference in Java Streams

PHP LARAVEL Directory Structure

PHP LARAVEL - Hello World Laravel