diff --git a/spring-rest-angular-pagination/src/main/java/org/baeldung/mock/MockStudentData.java b/spring-rest-angular-pagination/src/main/java/org/baeldung/mock/MockStudentData.java deleted file mode 100644 index 2c37317b75..0000000000 --- a/spring-rest-angular-pagination/src/main/java/org/baeldung/mock/MockStudentData.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.baeldung.mock; - -import java.util.ArrayList; -import java.util.List; - -import org.baeldung.web.entity.Student; - -public class MockStudentData { - - private static List studentList = new ArrayList<>(); - -// static { -// studentList.add(new Student("1", "Bryan", "Male", 20)); -// studentList.add(new Student("2", "Ben", "Male", 22)); -// studentList.add(new Student("3", "Lisa", "Female", 24)); -// studentList.add(new Student("4", "Sarah", "Female", 26)); -// studentList.add(new Student("5", "Jay", "Male", 20)); -// studentList.add(new Student("6", "John", "Male", 22)); -// studentList.add(new Student("7", "Jordan", "Male", 24)); -// studentList.add(new Student("8", "Rob", "Male", 26)); -// studentList.add(new Student("9", "Will", "Male", 20)); -// studentList.add(new Student("10", "Shawn", "Male", 22)); -// studentList.add(new Student("11", "Taylor", "Female", 24)); -// studentList.add(new Student("12", "Venus", "Female", 26)); -// studentList.add(new Student("13", "Vince", "Male", 20)); -// studentList.add(new Student("14", "Carol", "Female", 22)); -// studentList.add(new Student("15", "Joana", "Female", 24)); -// studentList.add(new Student("16", "Dion", "Male", 26)); -// studentList.add(new Student("17", "Evans", "Male", 20)); -// studentList.add(new Student("18", "Bart", "Male", 22)); -// studentList.add(new Student("19", "Jenny", "Female", 24)); -// studentList.add(new Student("20", "Kristine", "Female", 26)); -// } - - public static List getMockDataStudents(){ - return studentList; - } - -} diff --git a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/main/PersistenceConfig.java b/spring-rest-angular-pagination/src/main/java/org/baeldung/web/main/PersistenceConfig.java index 2a50f19907..c20664473b 100644 --- a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/main/PersistenceConfig.java +++ b/spring-rest-angular-pagination/src/main/java/org/baeldung/web/main/PersistenceConfig.java @@ -12,7 +12,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; -@EnableJpaRepositories("org.baeldung") +@EnableJpaRepositories("org.baeldung.web.dao") @ComponentScan(basePackages = { "org.baeldung.web" }) @EntityScan("org.baeldung.web.entity") @Configuration @@ -25,7 +25,6 @@ public class PersistenceConfig { @Bean public DataSource dataSource() { - EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); EmbeddedDatabase db = builder .setType(EmbeddedDatabaseType.HSQL) diff --git a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java b/spring-rest-angular-pagination/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java index 1aa9390aee..97a9d901c2 100644 --- a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java +++ b/spring-rest-angular-pagination/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java @@ -1,6 +1,7 @@ package org.baeldung.web.rest; import org.baeldung.web.entity.Student; +import org.baeldung.web.exception.MyResourceNotFoundException; import org.baeldung.web.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -15,11 +16,14 @@ public class StudentDirectoryRestController { @Autowired private StudentService service; - @RequestMapping(value = "/student/get", params = { "page", "size" }, method = RequestMethod.GET, produces = "application/json") - public Page findPaginated(@RequestParam("page") int page, @RequestParam("size") int size){ + @RequestMapping(value = "/student/get", params = { "page", + "size" }, method = RequestMethod.GET, produces = "application/json") + public Page findPaginated(@RequestParam("page") int page, @RequestParam("size") int size) { Page resultPage = service.findPaginated(page, size); - + if (page > resultPage.getTotalPages()) { + throw new MyResourceNotFoundException(); + } return resultPage; } diff --git a/spring-rest-angular-pagination/src/main/resources/application.properties b/spring-rest-angular-pagination/src/main/resources/application.properties index 9b4f95a9dc..cfabe45a42 100644 --- a/spring-rest-angular-pagination/src/main/resources/application.properties +++ b/spring-rest-angular-pagination/src/main/resources/application.properties @@ -1,3 +1,4 @@ server.contextPath=/ spring.h2.console.enabled=true -logging.level.org.hibernate.SQL=debug \ No newline at end of file +logging.level.org.hibernate.SQL=trace +spring.jpa.hibernate.ddl-auto=none \ No newline at end of file diff --git a/spring-rest-angular-pagination/src/main/resources/db/sql/data.sql b/spring-rest-angular-pagination/src/main/resources/db/sql/data.sql index d9f6ac610a..418381a681 100644 --- a/spring-rest-angular-pagination/src/main/resources/db/sql/data.sql +++ b/spring-rest-angular-pagination/src/main/resources/db/sql/data.sql @@ -6,4 +6,42 @@ CREATE TABLE student ( ); INSERT INTO student (id,name,gender,age) -VALUES (1,'Bryan', 'Male',20); \ No newline at end of file +VALUES (1,'Bryan', 'Male',20); +INSERT INTO student (id,name,gender,age) +VALUES (2, 'Ben', 'Male', 22); +INSERT INTO student (id,name,gender,age) +VALUES (3,'Lisa', 'Female',24); +INSERT INTO student (id,name,gender,age) +VALUES (4,'Sarah', 'Female',20); +INSERT INTO student (id,name,gender,age) +VALUES (5,'Jay', 'Male',20); +INSERT INTO student (id,name,gender,age) +VALUES (6,'John', 'Male',22); +INSERT INTO student (id,name,gender,age) +VALUES (7,'Jordan', 'Male',24); +INSERT INTO student (id,name,gender,age) +VALUES (8,'Rob', 'Male',26); +INSERT INTO student (id,name,gender,age) +VALUES (9,'Will', 'Male',20); +INSERT INTO student (id,name,gender,age) +VALUES (10,'Shawn', 'Male',22); +INSERT INTO student (id,name,gender,age) +VALUES (11,'Taylor', 'Female',24); +INSERT INTO student (id,name,gender,age) +VALUES (12,'Venus', 'Female',26); +INSERT INTO student (id,name,gender,age) +VALUES (13,'Vince', 'Male',20); +INSERT INTO student (id,name,gender,age) +VALUES (14,'Carol', 'Female',22); +INSERT INTO student (id,name,gender,age) +VALUES (15,'Joana', 'Female',24); +INSERT INTO student (id,name,gender,age) +VALUES (16,'Dion', 'Male',26); +INSERT INTO student (id,name,gender,age) +VALUES (17,'Evans', 'Male',20); +INSERT INTO student (id,name,gender,age) +VALUES (18,'Bart', 'Male',22); +INSERT INTO student (id,name,gender,age) +VALUES (19,'Jenny', 'Female',24); +INSERT INTO student (id,name,gender,age) +VALUES (20,'Kristine', 'Female',26); \ No newline at end of file diff --git a/spring-rest-angular-pagination/src/main/webapp/view/app.js b/spring-rest-angular-pagination/src/main/webapp/view/app.js index 715b667cc9..a41026d2c3 100644 --- a/spring-rest-angular-pagination/src/main/webapp/view/app.js +++ b/spring-rest-angular-pagination/src/main/webapp/view/app.js @@ -19,7 +19,7 @@ app.controller('StudentCtrl', ['$scope','StudentService', function ($scope,Stude enableColumnMenus:false, useExternalPagination: true, columnDefs: [ - { name: 'studentId' }, + { name: 'id' }, { name: 'name' }, { name: 'gender' }, { name: 'age' } @@ -42,6 +42,7 @@ app.controller('StudentCtrl', ['$scope','StudentService', function ($scope,Stude app.service('StudentService',['$http', function ($http) { function getStudents(pageNumber,size) { + pageNumber = pageNumber > 0?pageNumber - 1:0; return $http({ method: 'GET', url: 'student/get?page='+pageNumber+'&size='+size diff --git a/spring-rest-angular-pagination/src/test/java/org/baeldung/web/service/StudentServiceTest.java b/spring-rest-angular-pagination/src/test/java/org/baeldung/web/service/StudentServiceTest.java index 19fe77a1fd..a9417ad5b8 100644 --- a/spring-rest-angular-pagination/src/test/java/org/baeldung/web/service/StudentServiceTest.java +++ b/spring-rest-angular-pagination/src/test/java/org/baeldung/web/service/StudentServiceTest.java @@ -9,9 +9,10 @@ import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; -import static io.restassured.RestAssured.given; -import static org.hamcrest.core.IsCollectionContaining.hasItems; -import static org.hamcrest.core.IsEqual.equalTo; +import static io.restassured.RestAssured.*; +import static org.hamcrest.core.IsCollectionContaining.*; +import static org.hamcrest.core.Is.*; +import static org.hamcrest.core.IsEqual.*; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @@ -23,28 +24,28 @@ public class StudentServiceTest { @Test public void givenRequestForStudents_whenPageIsOne_expectContainsNames() { - given().params("page", "1", "size", "2").get(ENDPOINT) + given().params("page", "0", "size", "2").get(ENDPOINT) .then() .assertThat().body("content.name", hasItems("Bryan", "Ben")); } @Test public void givenRequestForStudents_whenSizeIsTwo_expectTwoItems() { - given().params("page", "1", "size", "2").get(ENDPOINT) + given().params("page", "0", "size", "2").get(ENDPOINT) .then() .assertThat().body("size", equalTo(2)); } @Test public void givenRequestForStudents_whenSizeIsTwo_expectNumberOfElementsTwo() { - given().params("page", "1", "size", "2").get(ENDPOINT) + given().params("page", "0", "size", "2").get(ENDPOINT) .then() .assertThat().body("numberOfElements", equalTo(2)); } @Test public void givenRequestForStudents_whenResourcesAreRetrievedPaged_thenExpect200() { - given().params("page", "1", "size", "2").get(ENDPOINT) + given().params("page", "0", "size", "2").get(ENDPOINT) .then() .statusCode(200); } @@ -64,15 +65,15 @@ public class StudentServiceTest { } @Test - public void givenRequestForStudents_whenPageIsFive_expectFiveItems() { - given().params("page", "1", "size", "5").get(ENDPOINT) + public void givenRequestForStudents_whenPageSizeIsFive_expectFiveItems() { + given().params("page", "0", "size", "5").get(ENDPOINT) .then() - .body("content.studentId.max()", equalTo("5")); + .body("content.size()", is(5)); } @Test public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() { - given().params("page", "1", "size", "2").get(ENDPOINT) + given().params("page", "0", "size", "2").get(ENDPOINT) .then() .assertThat().body("first", equalTo(true)); }