spring boot repository rest interface
10 Sep 2016Spring boot is an awesome “wrapper” which allows Spring projects to be created and run with minimal configuration and it makes development fun again. Recently I revisited Spring JPA with Spring Boot and Hibernate and discovered the wonders of exposing a Spring repository using REST.
Spring Repository
Defining a Spring Repository:
By Default defining a repository as above will create the following REST URL: http://localhost:8080/users
If we want to change the path of the default url created:
Now the URL will be: http://localhost:8080/users
This URl will allow CRUD actions using POST, PATCH, DELETE.
Sometimes the need arises to define custom methods for the JPA repository:
This will expose the following URL: http://localhost:8080/user/search/findByEmailAndPassword{?email,password}
Now let’s define the User Entity:
Now a GET for this entity will by default not return the id for the entity, in this case the email. To enable this create a configuration class as below:
And presto, everything just works as promised.