Skip to content

Commit 219e2f2

Browse files
committed
[spring-rest-angular-pagination]initial draft
1 parent ea86701 commit 219e2f2

14 files changed

Lines changed: 445 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>angular-spring-rest-sample</artifactId>
7+
<name>angular-spring-rest-sample</name>
8+
<groupId>com.baeldung</groupId>
9+
<version>1.0</version>
10+
<packaging>war</packaging>
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.3.3.RELEASE</version>
15+
</parent>
16+
<properties>
17+
<spring.data.version>1.12.2.RELEASE</spring.data.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-tomcat</artifactId>
23+
<scope>provided</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-web</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.data</groupId>
31+
<artifactId>spring-data-commons</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework</groupId>
35+
<artifactId>spring-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.apache.commons</groupId>
40+
<artifactId>commons-lang3</artifactId>
41+
<version>3.3</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.google.guava</groupId>
45+
<artifactId>guava</artifactId>
46+
<version>19.0</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>junit</groupId>
50+
<artifactId>junit</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.rest-assured</groupId>
55+
<artifactId>rest-assured</artifactId>
56+
<version>3.0.0</version>
57+
<scope>test</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>io.rest-assured</groupId>
61+
<artifactId>spring-mock-mvc</artifactId>
62+
<version>3.0.0</version>
63+
<scope>test</scope>
64+
</dependency>
65+
</dependencies>
66+
<build>
67+
<finalName>angular-spring-rest-sample</finalName>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-compiler-plugin</artifactId>
72+
<configuration>
73+
<source>1.8</source>
74+
<target>1.8</target>
75+
</configuration>
76+
</plugin>
77+
<plugin>
78+
<groupId>org.apache.maven.plugins</groupId>
79+
<artifactId>maven-war-plugin</artifactId>
80+
<configuration>
81+
<failOnMissingWebXml>false</failOnMissingWebXml>
82+
</configuration>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.baeldung.mock;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.baeldung.web.vo.Student;
7+
8+
public class MockStudentData {
9+
10+
private static List<Student> studentList = new ArrayList<>();
11+
12+
static {
13+
studentList.add(new Student("1", "Bryan", "Male", 20));
14+
studentList.add(new Student("2", "Ben", "Male", 22));
15+
studentList.add(new Student("3", "Lisa", "Female", 24));
16+
studentList.add(new Student("4", "Sarah", "Female", 26));
17+
studentList.add(new Student("5", "Jay", "Male", 20));
18+
studentList.add(new Student("6", "John", "Male", 22));
19+
studentList.add(new Student("7", "Jordan", "Male", 24));
20+
studentList.add(new Student("8", "Rob", "Male", 26));
21+
studentList.add(new Student("9", "Will", "Male", 20));
22+
studentList.add(new Student("10", "Shawn", "Male", 22));
23+
studentList.add(new Student("11", "Taylor", "Female", 24));
24+
studentList.add(new Student("12", "Venus", "Female", 26));
25+
studentList.add(new Student("13", "Vince", "Male", 20));
26+
studentList.add(new Student("14", "Carol", "Female", 22));
27+
studentList.add(new Student("15", "Joana", "Female", 24));
28+
studentList.add(new Student("16", "Dion", "Male", 26));
29+
studentList.add(new Student("17", "Evans", "Male", 20));
30+
studentList.add(new Student("18", "Bart", "Male", 22));
31+
studentList.add(new Student("19", "Jenny", "Female", 24));
32+
studentList.add(new Student("20", "Kristine", "Female", 26));
33+
}
34+
35+
public static List<Student> getMockDataStudents(){
36+
return studentList;
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.baeldung.web.exception;
2+
3+
public class MyResourceNotFoundException extends RuntimeException {
4+
5+
/**
6+
*
7+
*/
8+
private static final long serialVersionUID = 4088649120307193208L;
9+
10+
public MyResourceNotFoundException() {
11+
super();
12+
}
13+
14+
public MyResourceNotFoundException(final String message, final Throwable cause) {
15+
super(message, cause);
16+
}
17+
18+
public MyResourceNotFoundException(final String message) {
19+
super(message);
20+
}
21+
22+
public MyResourceNotFoundException(final Throwable cause) {
23+
super(cause);
24+
}
25+
26+
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.web.main;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.web.filter.ShallowEtagHeaderFilter;
9+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10+
11+
@SpringBootApplication
12+
@EnableAutoConfiguration
13+
@ComponentScan("org.baeldung")
14+
public class Application extends WebMvcConfigurerAdapter {
15+
16+
17+
public static void main(String[] args) {
18+
SpringApplication.run(Application.class, args);
19+
}
20+
21+
@Bean
22+
public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
23+
return new ShallowEtagHeaderFilter();
24+
}
25+
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.web.rest;
2+
3+
import org.baeldung.web.service.StudentService;
4+
import org.baeldung.web.vo.Student;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.data.domain.Page;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@RestController
13+
public class StudentDirectoryRestController {
14+
15+
@Autowired
16+
private StudentService service;
17+
18+
@RequestMapping(value = "/student/get", params = { "page", "size" }, method = RequestMethod.GET)
19+
public Page<Student> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size){
20+
21+
Page<Student> resultPage = service.findPaginated(page, size);
22+
23+
return resultPage;
24+
}
25+
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.web.service;
2+
3+
import org.springframework.data.domain.Page;
4+
5+
public interface IOperations<T> {
6+
7+
Page<T> findPaginated(int page, int size);
8+
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.baeldung.web.service;
2+
3+
import org.baeldung.web.vo.Student;
4+
5+
public interface StudentService extends IOperations<Student>{
6+
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.baeldung.web.service;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.mock.MockStudentData;
6+
import org.baeldung.web.exception.MyResourceNotFoundException;
7+
import org.baeldung.web.vo.Student;
8+
import org.springframework.data.domain.Page;
9+
import org.springframework.data.domain.PageImpl;
10+
import org.springframework.data.domain.PageRequest;
11+
import org.springframework.stereotype.Service;
12+
13+
@Service
14+
public class StudentServiceImpl implements StudentService {
15+
16+
private List<Student> mockDataStudent = MockStudentData.getMockDataStudents();
17+
18+
@Override
19+
public Page<Student> findPaginated(int page, int size){
20+
Page<Student> studentPage = getPage(page, size);
21+
return studentPage;
22+
}
23+
24+
private Page<Student> getPage(int page, int size) {
25+
page = page != 0?page - 1:page;
26+
int from = Math.max(0, page * size);
27+
int to = Math.min(mockDataStudent.size(), (page + 1) * size);
28+
if(from > to){
29+
throw new MyResourceNotFoundException("page number is higher than total pages.");
30+
}
31+
return new PageImpl<Student>(mockDataStudent.subList(from, to),
32+
new PageRequest(page,size),
33+
mockDataStudent.size());
34+
}
35+
36+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.baeldung.web.vo;
2+
3+
import java.io.Serializable;
4+
5+
public class Student implements Serializable {
6+
7+
/**
8+
*
9+
*/
10+
private static final long serialVersionUID = 1L;
11+
12+
public Student() {
13+
}
14+
15+
public Student(String studentId, String name, String gender, Integer age) {
16+
super();
17+
this.studentId = studentId;
18+
this.name = name;
19+
this.gender = gender;
20+
this.age = age;
21+
}
22+
23+
private String studentId;
24+
private String name;
25+
private String gender;
26+
private Integer age;
27+
28+
public String getStudentId() {
29+
return studentId;
30+
}
31+
32+
public void setStudentId(String studentId) {
33+
this.studentId = studentId;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public void setName(String name) {
41+
this.name = name;
42+
}
43+
44+
public String getGender() {
45+
return gender;
46+
}
47+
48+
public void setGender(String gender) {
49+
this.gender = gender;
50+
}
51+
52+
public Integer getAge() {
53+
return age;
54+
}
55+
56+
public void setAge(Integer age) {
57+
this.age = age;
58+
}
59+
60+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.contextPath=/StudentDirectory

0 commit comments

Comments
 (0)