Spring Data REST using Spring Data JPA
Overview:
In this tutorial we would be exposing the Spring Data JPA repositories in the form of REST repositories.
Spring Data JPA can’t directly talk with database, they need some JPA providers (like Hibernate, iBatis) to talk with the database.
You may ask why the hell we need another layer in between(in the form of Spring Data REST) to interact with the DB, hibernate is good enough. The answer to this is – tomorrow if you want to change your JPA provider from Hibernate to some other providers, it would be easy. You may now ask then why does Spring doesn’t have its own JPA providers and talk directly with the database. The answer to this is- yes, Spring has its Spring ORM module. But its better to have a JPA layer talk with these JPA providers(ORM tools) which then should interact with the database.
In this tutorial we would be using Spring Boot to avoid any boiler plate code.
Spring Data Rest is build on Spring Data JPA. And all these uses Hipermedia(Hypertext Markup Language) as the MIME type.
Below dependencies would be required for our application:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> </dependencies>
Now lets have our single page application containing everything:
package com.heapsteep.demo; import java.util.List; import java.util.stream.Stream; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @Entity @Builder @NoArgsConstructor @AllArgsConstructor @Setter @Getter class User { @Id private long id; private String name; private String email; } @RepositoryRestResource interface UserRepository extends JpaRepository<User, Long> {} @Component class UserInitializer implements CommandLineRunner{ private final UserRepository userRepository; UserInitializer(UserRepository userRepository) { this.userRepository = userRepository; } @Override public void run(String... args) throws Exception { // TODO Auto-generated method stub userRepository.save(new User(1, "Adam", "[email protected]")); userRepository.save(new User(2, "Bob", "[email protected]")); } } @SpringBootApplication public class SpringDataRestApplication { public static void main(String[] args) { SpringApplication.run(SpringDataRestApplication.class, args); } }
Now lets test our application.
We will be using any of the tool like Postman, SOAPUI etc.
In this example we will be using SOAPUI tool to call our Rest services. (Please note that this tool can be used not only to call SOAP services, but also to call Rest services as well)
Hit this URL:
http://localhost:8080/users
Result:
Full pic of the tool:
if you hit this url:
http://localhost:8080/users/1