spring data rest projections
This commit is contained in:
parent
238618abb5
commit
79ab39d9f1
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<Author> 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;
|
||||
}
|
||||
|
|
|
@ -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<Author> getAuthors();
|
||||
|
||||
@Value("#{target.getAuthors().size()}")
|
||||
int getAuthorCount();
|
||||
}
|
|
@ -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<Book, Long> {
|
||||
|
||||
}
|
||||
@RepositoryRestResource(excerptProjection = CustomBook.class)
|
||||
public interface BookRepository extends CrudRepository<Book, Long> {}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue