From 79ab39d9f18024cb5696da31ebe5a12f4ce22982 Mon Sep 17 00:00:00 2001 From: DOHA Date: Sat, 28 Apr 2018 15:52:27 +0300 Subject: [PATCH] spring data rest projections --- .../java/com/baeldung/config/RestConfig.java | 17 ++++ .../main/java/com/baeldung/models/Book.java | 16 +++- .../com/baeldung/projections/CustomBook.java | 22 +++++ .../baeldung/repositories/BookRepository.java | 7 +- .../SpringDataProjectionIntegrationTest.java | 92 +++++++++++++++++++ 5 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 spring-data-rest/src/main/java/com/baeldung/config/RestConfig.java create mode 100644 spring-data-rest/src/main/java/com/baeldung/projections/CustomBook.java create mode 100644 spring-data-rest/src/test/java/com/baeldung/projection/SpringDataProjectionIntegrationTest.java diff --git a/spring-data-rest/src/main/java/com/baeldung/config/RestConfig.java b/spring-data-rest/src/main/java/com/baeldung/config/RestConfig.java new file mode 100644 index 0000000000..7434dde394 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/config/RestConfig.java @@ -0,0 +1,17 @@ +package com.baeldung.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.data.rest.core.config.RepositoryRestConfiguration; +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; + +import com.baeldung.projections.CustomBook; + + +@Configuration +public class RestConfig extends RepositoryRestConfigurerAdapter{ + + @Override + public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repositoryRestConfiguration){ + repositoryRestConfiguration.getProjectionConfiguration().addProjection(CustomBook.class); + } +} diff --git a/spring-data-rest/src/main/java/com/baeldung/models/Book.java b/spring-data-rest/src/main/java/com/baeldung/models/Book.java index 06abfb8f4d..002a64e738 100644 --- a/spring-data-rest/src/main/java/com/baeldung/models/Book.java +++ b/spring-data-rest/src/main/java/com/baeldung/models/Book.java @@ -4,6 +4,7 @@ import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @@ -20,12 +21,14 @@ public class Book { @Column(nullable = false) private String title; - + + private String isbn; + @ManyToOne @JoinColumn(name = "library_id") private Library library; - @ManyToMany(mappedBy = "books") + @ManyToMany(mappedBy = "books", fetch = FetchType.EAGER) private List authors; public Book() { @@ -52,6 +55,15 @@ public class Book { this.id = id; } + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + public Library getLibrary() { return library; } diff --git a/spring-data-rest/src/main/java/com/baeldung/projections/CustomBook.java b/spring-data-rest/src/main/java/com/baeldung/projections/CustomBook.java new file mode 100644 index 0000000000..1cd9c01383 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/projections/CustomBook.java @@ -0,0 +1,22 @@ +package com.baeldung.projections; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.rest.core.config.Projection; + +import com.baeldung.models.Author; +import com.baeldung.models.Book; + +@Projection(name = "customBook", types = { Book.class }) +public interface CustomBook { + @Value("#{target.id}") + long getId(); + + String getTitle(); + + List getAuthors(); + + @Value("#{target.getAuthors().size()}") + int getAuthorCount(); +} diff --git a/spring-data-rest/src/main/java/com/baeldung/repositories/BookRepository.java b/spring-data-rest/src/main/java/com/baeldung/repositories/BookRepository.java index f9176032ab..34019a9d91 100644 --- a/spring-data-rest/src/main/java/com/baeldung/repositories/BookRepository.java +++ b/spring-data-rest/src/main/java/com/baeldung/repositories/BookRepository.java @@ -1,9 +1,10 @@ package com.baeldung.repositories; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; import com.baeldung.models.Book; +import com.baeldung.projections.CustomBook; -public interface BookRepository extends CrudRepository { - -} +@RepositoryRestResource(excerptProjection = CustomBook.class) +public interface BookRepository extends CrudRepository {} diff --git a/spring-data-rest/src/test/java/com/baeldung/projection/SpringDataProjectionIntegrationTest.java b/spring-data-rest/src/test/java/com/baeldung/projection/SpringDataProjectionIntegrationTest.java new file mode 100644 index 0000000000..4091fdf154 --- /dev/null +++ b/spring-data-rest/src/test/java/com/baeldung/projection/SpringDataProjectionIntegrationTest.java @@ -0,0 +1,92 @@ +package com.baeldung.projection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import io.restassured.RestAssured; +import io.restassured.response.Response; + +import java.util.Arrays; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.SpringDataRestApplication; +import com.baeldung.models.Author; +import com.baeldung.models.Book; +import com.baeldung.repositories.AuthorRepository; +import com.baeldung.repositories.BookRepository; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SpringDataRestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT) + +public class SpringDataProjectionIntegrationTest { + private static final String BOOK_ENDPOINT = "http://localhost:8080/books"; + private static final String AUTHOR_ENDPOINT = "http://localhost:8080/authors"; + + + @Autowired + private BookRepository bookRepo; + + @Autowired + private AuthorRepository authorRepo; + + @Before + public void setup(){ + if(bookRepo.findOne(1L) == null){ + Book book = new Book("Animal Farm"); + book.setIsbn("978-1943138425"); + book = bookRepo.save(book); + Author author = new Author("George Orwell"); + author = authorRepo.save(author); + author.setBooks(Arrays.asList(book)); + author = authorRepo.save(author); + } + } + + @Test + public void whenGetBook_thenOK(){ + final Response response = RestAssured.get(BOOK_ENDPOINT+"/1"); + + assertEquals(200, response.getStatusCode()); + assertTrue(response.asString().contains("isbn")); + assertFalse(response.asString().contains("authorCount")); +// System.out.println(response.asString()); + } + + + @Test + public void whenGetBookProjection_thenOK(){ + final Response response = RestAssured.get(BOOK_ENDPOINT+"/1?projection=customBook"); + + assertEquals(200, response.getStatusCode()); + assertFalse(response.asString().contains("isbn")); + assertTrue(response.asString().contains("authorCount")); +// System.out.println(response.asString()); + } + + @Test + public void whenGetAllBooks_thenOK(){ + final Response response = RestAssured.get(BOOK_ENDPOINT); + + assertEquals(200, response.getStatusCode()); + assertFalse(response.asString().contains("isbn")); + assertTrue(response.asString().contains("authorCount")); + // System.out.println(response.asString()); + } + + @Test + public void whenGetAuthorBooks_thenOK(){ + final Response response = RestAssured.get(AUTHOR_ENDPOINT+"/1/books"); + + assertEquals(200, response.getStatusCode()); + assertFalse(response.asString().contains("isbn")); + assertTrue(response.asString().contains("authorCount")); + System.out.println(response.asString()); + } +}