diff --git a/mapstruct/pom.xml b/mapstruct/pom.xml
new file mode 100644
index 0000000000..8a28ae9511
--- /dev/null
+++ b/mapstruct/pom.xml
@@ -0,0 +1,49 @@
+
+
+ 4.0.0
+ mapstruct
+ mapstruct
+ com.baeldung
+ 1.0
+ jar
+
+
+ 1.0.0.Final
+
+
+
+ org.mapstruct
+ mapstruct-jdk8
+ ${org.mapstruct.version}
+
+
+ junit
+ junit
+ 4.12
+ test
+
+
+
+ mapstruct
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.5.1
+
+
+ 1.8
+
+
+ org.mapstruct
+ mapstruct-processor
+ ${org.mapstruct.version}
+
+
+
+
+
+
+
diff --git a/mapstruct/src/main/java/org/baeldung/dto/EmployeeDTO.java b/mapstruct/src/main/java/org/baeldung/dto/EmployeeDTO.java
new file mode 100644
index 0000000000..67b9ffc024
--- /dev/null
+++ b/mapstruct/src/main/java/org/baeldung/dto/EmployeeDTO.java
@@ -0,0 +1,42 @@
+package org.baeldung.dto;
+
+public class EmployeeDTO {
+
+ private int employeeId;
+ private String employeeName;
+ private int divisionId;
+ private String divisionName;
+
+ public int getEmployeeId() {
+ return employeeId;
+ }
+
+ public void setEmployeeId(int employeeId) {
+ this.employeeId = employeeId;
+ }
+
+ public String getEmployeeName() {
+ return employeeName;
+ }
+
+ public void setEmployeeName(String employeeName) {
+ this.employeeName = employeeName;
+ }
+
+ public int getDivisionId() {
+ return divisionId;
+ }
+
+ public void setDivisionId(int divisionId) {
+ this.divisionId = divisionId;
+ }
+
+ public String getDivisionName() {
+ return divisionName;
+ }
+
+ public void setDivisionName(String divisionName) {
+ this.divisionName = divisionName;
+ }
+
+}
diff --git a/mapstruct/src/main/java/org/baeldung/dto/SimpleSource.java b/mapstruct/src/main/java/org/baeldung/dto/SimpleSource.java
new file mode 100644
index 0000000000..fc0c75dea3
--- /dev/null
+++ b/mapstruct/src/main/java/org/baeldung/dto/SimpleSource.java
@@ -0,0 +1,24 @@
+package org.baeldung.dto;
+
+public class SimpleSource {
+
+ private String name;
+ private String description;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+}
diff --git a/mapstruct/src/main/java/org/baeldung/entity/Division.java b/mapstruct/src/main/java/org/baeldung/entity/Division.java
new file mode 100644
index 0000000000..04f1ab2c23
--- /dev/null
+++ b/mapstruct/src/main/java/org/baeldung/entity/Division.java
@@ -0,0 +1,33 @@
+package org.baeldung.entity;
+
+public class Division {
+
+ public Division() {
+ }
+
+ public Division(int id, String name) {
+ super();
+ this.id = id;
+ this.name = name;
+ }
+
+ private int id;
+ private String name;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+}
diff --git a/mapstruct/src/main/java/org/baeldung/entity/Employee.java b/mapstruct/src/main/java/org/baeldung/entity/Employee.java
new file mode 100644
index 0000000000..55599a17f9
--- /dev/null
+++ b/mapstruct/src/main/java/org/baeldung/entity/Employee.java
@@ -0,0 +1,33 @@
+package org.baeldung.entity;
+
+public class Employee {
+
+ private int id;
+ private String name;
+ private Division division;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public Division getDivision() {
+ return division;
+ }
+
+ public void setDivision(Division division) {
+ this.division = division;
+ }
+
+}
diff --git a/mapstruct/src/main/java/org/baeldung/entity/SimpleDestination.java b/mapstruct/src/main/java/org/baeldung/entity/SimpleDestination.java
new file mode 100644
index 0000000000..901257c11b
--- /dev/null
+++ b/mapstruct/src/main/java/org/baeldung/entity/SimpleDestination.java
@@ -0,0 +1,24 @@
+package org.baeldung.entity;
+
+public class SimpleDestination {
+
+ private String name;
+ private String description;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+}
diff --git a/mapstruct/src/main/java/org/baeldung/mapper/EmployeeMapper.java b/mapstruct/src/main/java/org/baeldung/mapper/EmployeeMapper.java
new file mode 100644
index 0000000000..28faffec45
--- /dev/null
+++ b/mapstruct/src/main/java/org/baeldung/mapper/EmployeeMapper.java
@@ -0,0 +1,27 @@
+package org.baeldung.mapper;
+
+import org.baeldung.dto.EmployeeDTO;
+import org.baeldung.entity.Employee;
+import org.mapstruct.Mapper;
+import org.mapstruct.Mapping;
+import org.mapstruct.Mappings;
+
+@Mapper
+public interface EmployeeMapper {
+
+ @Mappings({
+ @Mapping(target="divisionId",source="entity.division.id"),
+ @Mapping(target="divisionName",source="entity.division.name"),
+ @Mapping(target="employeeId",source="entity.id"),
+ @Mapping(target="employeeName",source="entity.name")
+ })
+ EmployeeDTO employeeToEmployeeDTO(Employee entity);
+
+ @Mappings({
+ @Mapping(target="id",source="dto.employeeId"),
+ @Mapping(target="name",source="dto.employeeName"),
+ @Mapping(target="division",expression="java(new org.baeldung.entity.Division(dto.getDivisionId(),dto.getDivisionName()))")
+ })
+ Employee employeeDTOtoEmployee(EmployeeDTO dto);
+
+}
diff --git a/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationMapper.java b/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationMapper.java
new file mode 100644
index 0000000000..f3210288d2
--- /dev/null
+++ b/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationMapper.java
@@ -0,0 +1,13 @@
+package org.baeldung.mapper;
+
+import org.baeldung.dto.SimpleSource;
+import org.baeldung.entity.SimpleDestination;
+import org.mapstruct.Mapper;
+
+@Mapper
+public interface SimpleSourceDestinationMapper {
+
+ SimpleDestination sourceToDestination(SimpleSource source);
+ SimpleSource destinationToSource(SimpleDestination destination);
+
+}
diff --git a/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationSpringMapper.java b/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationSpringMapper.java
new file mode 100644
index 0000000000..2077eabf51
--- /dev/null
+++ b/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationSpringMapper.java
@@ -0,0 +1,13 @@
+package org.baeldung.mapper;
+
+import org.baeldung.dto.SimpleSource;
+import org.baeldung.entity.SimpleDestination;
+import org.mapstruct.Mapper;
+
+@Mapper(componentModel="spring")
+public interface SimpleSourceDestinationSpringMapper {
+
+ SimpleDestination sourceToDestination(SimpleSource source);
+ SimpleSource destinationToSource(SimpleDestination destination);
+
+}
diff --git a/mapstruct/src/test/java/org/baeldung/mapper/EmployeeMapperTest.java b/mapstruct/src/test/java/org/baeldung/mapper/EmployeeMapperTest.java
new file mode 100644
index 0000000000..0433013f2e
--- /dev/null
+++ b/mapstruct/src/test/java/org/baeldung/mapper/EmployeeMapperTest.java
@@ -0,0 +1,48 @@
+package org.baeldung.mapper;
+
+import static org.junit.Assert.*;
+
+import org.baeldung.dto.EmployeeDTO;
+import org.baeldung.entity.Division;
+import org.baeldung.entity.Employee;
+import org.junit.Test;
+import org.mapstruct.factory.Mappers;
+
+public class EmployeeMapperTest {
+
+ @Test
+ public void givenEmployeeDTOtoEmployee_whenMaps_thenCorrect(){
+ EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
+
+ EmployeeDTO dto = new EmployeeDTO();
+ dto.setDivisionId(1);
+ dto.setDivisionName("IT Division");
+ dto.setEmployeeId(1);
+ dto.setEmployeeName("John");
+
+ Employee entity = mapper.employeeDTOtoEmployee(dto);
+
+ assertEquals(dto.getDivisionId(), entity.getDivision().getId());
+ assertEquals(dto.getDivisionName(), entity.getDivision().getName());
+ assertEquals(dto.getEmployeeId(),entity.getId());
+ assertEquals(dto.getEmployeeName(),entity.getName());
+ }
+
+ @Test
+ public void givenEmployeetoEmployeeDTO_whenMaps_thenCorrect(){
+ EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
+
+ Employee entity = new Employee();
+ entity.setDivision(new Division(1,"IT Division"));
+ entity.setId(1);
+ entity.setName("John");
+
+ EmployeeDTO dto = mapper.employeeToEmployeeDTO(entity);
+
+ assertEquals(dto.getDivisionId(), entity.getDivision().getId());
+ assertEquals(dto.getDivisionName(), entity.getDivision().getName());
+ assertEquals(dto.getEmployeeId(),entity.getId());
+ assertEquals(dto.getEmployeeName(),entity.getName());
+ }
+
+}
diff --git a/mapstruct/src/test/java/org/baeldung/mapper/SimpleSourceDestinationMapperTest.java b/mapstruct/src/test/java/org/baeldung/mapper/SimpleSourceDestinationMapperTest.java
new file mode 100644
index 0000000000..a7e3b05dc6
--- /dev/null
+++ b/mapstruct/src/test/java/org/baeldung/mapper/SimpleSourceDestinationMapperTest.java
@@ -0,0 +1,44 @@
+package org.baeldung.mapper;
+
+import static org.junit.Assert.*;
+
+import org.baeldung.dto.SimpleSource;
+import org.baeldung.entity.SimpleDestination;
+import org.junit.Test;
+import org.mapstruct.factory.Mappers;
+
+public class SimpleSourceDestinationMapperTest {
+
+ @Test
+ public void givenSimpleSourceToSimpleDestination_whenMaps_thenCorrect() {
+ SimpleSourceDestinationMapper simpleSourceDestinationMapper = Mappers
+ .getMapper(SimpleSourceDestinationMapper.class);
+
+ SimpleSource simpleSource = new SimpleSource();
+ simpleSource.setName("SourceName");
+ simpleSource.setDescription("SourceDescription");
+
+ SimpleDestination destination =
+ simpleSourceDestinationMapper.sourceToDestination(simpleSource);
+
+ assertEquals(simpleSource.getName(), destination.getName());
+ assertEquals(simpleSource.getDescription(), destination.getDescription());
+ }
+
+ @Test
+ public void givenSimpleDestinationToSourceDestination_whenMaps_thenCorrect() {
+ SimpleSourceDestinationMapper simpleSourceDestinationMapper = Mappers
+ .getMapper(SimpleSourceDestinationMapper.class);
+
+ SimpleDestination destination = new SimpleDestination();
+ destination.setName("DestinationName");
+ destination.setDescription("DestinationDescription");
+
+ SimpleSource source =
+ simpleSourceDestinationMapper.destinationToSource(destination);
+
+ assertEquals(destination.getName(), source.getName());
+ assertEquals(destination.getDescription(), source.getDescription());
+ }
+
+}
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 df70780a87..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.vo.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/rest/StudentDirectoryRestController.java b/spring-rest-angular-pagination/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java
deleted file mode 100644
index b655d401a5..0000000000
--- a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java
+++ /dev/null
@@ -1,29 +0,0 @@
-package org.baeldung.web.rest;
-
-import org.baeldung.web.service.StudentService;
-import org.baeldung.web.vo.Student;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.domain.Page;
-import org.springframework.http.MediaType;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
-
-import static org.springframework.http.MediaType.APPLICATION_JSON;
-
-@RestController
-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){
-
- Page resultPage = service.findPaginated(page, size);
-
- return resultPage;
- }
-
-}
diff --git a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/service/StudentService.java b/spring-rest-angular-pagination/src/main/java/org/baeldung/web/service/StudentService.java
deleted file mode 100644
index 5c4487254a..0000000000
--- a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/service/StudentService.java
+++ /dev/null
@@ -1,7 +0,0 @@
-package org.baeldung.web.service;
-
-import org.baeldung.web.vo.Student;
-
-public interface StudentService extends IOperations{
-
-}
diff --git a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/service/StudentServiceImpl.java b/spring-rest-angular-pagination/src/main/java/org/baeldung/web/service/StudentServiceImpl.java
deleted file mode 100644
index 3b6dda6fb1..0000000000
--- a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/service/StudentServiceImpl.java
+++ /dev/null
@@ -1,36 +0,0 @@
-package org.baeldung.web.service;
-
-import java.util.List;
-
-import org.baeldung.mock.MockStudentData;
-import org.baeldung.web.exception.MyResourceNotFoundException;
-import org.baeldung.web.vo.Student;
-import org.springframework.data.domain.Page;
-import org.springframework.data.domain.PageImpl;
-import org.springframework.data.domain.PageRequest;
-import org.springframework.stereotype.Service;
-
-@Service
-public class StudentServiceImpl implements StudentService {
-
- private List mockDataStudent = MockStudentData.getMockDataStudents();
-
- @Override
- public Page findPaginated(int page, int size){
- Page studentPage = getPage(page, size);
- return studentPage;
- }
-
- private Page getPage(int page, int size) {
- page = page != 0?page - 1:page;
- int from = Math.max(0, page * size);
- int to = Math.min(mockDataStudent.size(), (page + 1) * size);
- if(from > to){
- throw new MyResourceNotFoundException("page number is higher than total pages.");
- }
- return new PageImpl(mockDataStudent.subList(from, to),
- new PageRequest(page,size),
- mockDataStudent.size());
- }
-
-}
diff --git a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/vo/Student.java b/spring-rest-angular-pagination/src/main/java/org/baeldung/web/vo/Student.java
deleted file mode 100644
index 11c503815d..0000000000
--- a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/vo/Student.java
+++ /dev/null
@@ -1,60 +0,0 @@
-package org.baeldung.web.vo;
-
-import java.io.Serializable;
-
-public class Student implements Serializable {
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
- public Student() {
- }
-
- public Student(String studentId, String name, String gender, Integer age) {
- super();
- this.studentId = studentId;
- this.name = name;
- this.gender = gender;
- this.age = age;
- }
-
- private String studentId;
- private String name;
- private String gender;
- private Integer age;
-
- public String getStudentId() {
- return studentId;
- }
-
- public void setStudentId(String studentId) {
- this.studentId = studentId;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getGender() {
- return gender;
- }
-
- public void setGender(String gender) {
- this.gender = gender;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
-}
diff --git a/spring-rest-angular-pagination/src/main/resources/application.properties b/spring-rest-angular-pagination/src/main/resources/application.properties
deleted file mode 100644
index e42588cee0..0000000000
--- a/spring-rest-angular-pagination/src/main/resources/application.properties
+++ /dev/null
@@ -1 +0,0 @@
-server.contextPath=/
\ No newline at end of file
diff --git a/spring-rest-angular-pagination/src/main/webapp/index.html b/spring-rest-angular-pagination/src/main/webapp/index.html
deleted file mode 100644
index 56a1273588..0000000000
--- a/spring-rest-angular-pagination/src/main/webapp/index.html
+++ /dev/null
@@ -1,14 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/spring-rest-angular-pagination/pom.xml b/spring-rest-angular/pom.xml
similarity index 87%
rename from spring-rest-angular-pagination/pom.xml
rename to spring-rest-angular/pom.xml
index 7a0f3e7b31..331c9a644c 100644
--- a/spring-rest-angular-pagination/pom.xml
+++ b/spring-rest-angular/pom.xml
@@ -3,8 +3,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4.0.0
- angular-spring-rest-sample
- angular-spring-rest-sample
+ spring-rest-angular
+ spring-rest-angular
com.baeldung
1.0
war
@@ -13,9 +13,6 @@
spring-boot-starter-parent
1.3.3.RELEASE
-
- 1.12.2.RELEASE
-
org.springframework.boot
@@ -30,6 +27,15 @@
org.springframework.data
spring-data-commons
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
+
+ org.hsqldb
+ hsqldb
+ runtime
+
org.springframework
spring-test
diff --git a/spring-rest-angular/src/main/java/org/baeldung/web/dao/StudentRepository.java b/spring-rest-angular/src/main/java/org/baeldung/web/dao/StudentRepository.java
new file mode 100644
index 0000000000..b1aafb583a
--- /dev/null
+++ b/spring-rest-angular/src/main/java/org/baeldung/web/dao/StudentRepository.java
@@ -0,0 +1,8 @@
+package org.baeldung.web.dao;
+
+import org.baeldung.web.entity.Student;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+public interface StudentRepository extends JpaRepository {
+
+}
diff --git a/spring-rest-angular/src/main/java/org/baeldung/web/entity/Student.java b/spring-rest-angular/src/main/java/org/baeldung/web/entity/Student.java
new file mode 100644
index 0000000000..0a0b60d87d
--- /dev/null
+++ b/spring-rest-angular/src/main/java/org/baeldung/web/entity/Student.java
@@ -0,0 +1,69 @@
+package org.baeldung.web.entity;
+
+import java.io.Serializable;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class Student implements Serializable {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+
+ public Student() {
+ }
+
+ public Student(long id, String name, String gender, Integer age) {
+ super();
+ this.id = id;
+ this.name = name;
+ this.gender = gender;
+ this.age = age;
+ }
+
+ @Id
+ private long id;
+ @Column(nullable = false)
+ private String name;
+ @Column(nullable = false)
+ private String gender;
+ @Column(nullable = false)
+ private Integer age;
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getGender() {
+ return gender;
+ }
+
+ public void setGender(String gender) {
+ this.gender = gender;
+ }
+
+ public Integer getAge() {
+ return age;
+ }
+
+ public void setAge(Integer age) {
+ this.age = age;
+ }
+
+}
diff --git a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/exception/MyResourceNotFoundException.java b/spring-rest-angular/src/main/java/org/baeldung/web/exception/MyResourceNotFoundException.java
similarity index 77%
rename from spring-rest-angular-pagination/src/main/java/org/baeldung/web/exception/MyResourceNotFoundException.java
rename to spring-rest-angular/src/main/java/org/baeldung/web/exception/MyResourceNotFoundException.java
index 3105d1cb11..740caec59e 100644
--- a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/exception/MyResourceNotFoundException.java
+++ b/spring-rest-angular/src/main/java/org/baeldung/web/exception/MyResourceNotFoundException.java
@@ -3,11 +3,11 @@ package org.baeldung.web.exception;
public class MyResourceNotFoundException extends RuntimeException {
/**
- *
- */
- private static final long serialVersionUID = 4088649120307193208L;
+ *
+ */
+ private static final long serialVersionUID = 4088649120307193208L;
- public MyResourceNotFoundException() {
+ public MyResourceNotFoundException() {
super();
}
@@ -23,5 +23,4 @@ public class MyResourceNotFoundException extends RuntimeException {
super(cause);
}
-
}
diff --git a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/main/Application.java b/spring-rest-angular/src/main/java/org/baeldung/web/main/Application.java
similarity index 88%
rename from spring-rest-angular-pagination/src/main/java/org/baeldung/web/main/Application.java
rename to spring-rest-angular/src/main/java/org/baeldung/web/main/Application.java
index b3b0dad98a..d6fe719311 100644
--- a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/main/Application.java
+++ b/spring-rest-angular/src/main/java/org/baeldung/web/main/Application.java
@@ -4,16 +4,15 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Import;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication
@EnableAutoConfiguration
-@ComponentScan("org.baeldung")
+@Import(PersistenceConfig.class)
public class Application extends WebMvcConfigurerAdapter {
-
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@@ -22,5 +21,5 @@ public class Application extends WebMvcConfigurerAdapter {
public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
return new ShallowEtagHeaderFilter();
}
-
+
}
diff --git a/spring-rest-angular/src/main/java/org/baeldung/web/main/PersistenceConfig.java b/spring-rest-angular/src/main/java/org/baeldung/web/main/PersistenceConfig.java
new file mode 100644
index 0000000000..df1240f270
--- /dev/null
+++ b/spring-rest-angular/src/main/java/org/baeldung/web/main/PersistenceConfig.java
@@ -0,0 +1,33 @@
+package org.baeldung.web.main;
+
+import javax.sql.DataSource;
+
+import org.springframework.boot.orm.jpa.EntityScan;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
+
+@EnableJpaRepositories("org.baeldung.web.dao")
+@ComponentScan(basePackages = { "org.baeldung.web" })
+@EntityScan("org.baeldung.web.entity")
+@Configuration
+public class PersistenceConfig {
+
+ @Bean
+ public JdbcTemplate getJdbcTemplate() {
+ return new JdbcTemplate(dataSource());
+ }
+
+ @Bean
+ public DataSource dataSource() {
+ EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
+ EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).addScript("db/sql/data.sql").build();
+ return db;
+ }
+
+}
diff --git a/spring-rest-angular/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java b/spring-rest-angular/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java
new file mode 100644
index 0000000000..dc295a3d97
--- /dev/null
+++ b/spring-rest-angular/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java
@@ -0,0 +1,29 @@
+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;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+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) {
+
+ Page resultPage = service.findPaginated(page, size);
+ if (page > resultPage.getTotalPages()) {
+ throw new MyResourceNotFoundException();
+ }
+ return resultPage;
+ }
+
+}
diff --git a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/service/IOperations.java b/spring-rest-angular/src/main/java/org/baeldung/web/service/IOperations.java
similarity index 64%
rename from spring-rest-angular-pagination/src/main/java/org/baeldung/web/service/IOperations.java
rename to spring-rest-angular/src/main/java/org/baeldung/web/service/IOperations.java
index 0b408106ce..2176c0bb70 100644
--- a/spring-rest-angular-pagination/src/main/java/org/baeldung/web/service/IOperations.java
+++ b/spring-rest-angular/src/main/java/org/baeldung/web/service/IOperations.java
@@ -4,6 +4,6 @@ import org.springframework.data.domain.Page;
public interface IOperations {
- Page findPaginated(int page, int size);
+ public Page findPaginated(final int page, final int size);
}
diff --git a/spring-rest-angular/src/main/java/org/baeldung/web/service/StudentService.java b/spring-rest-angular/src/main/java/org/baeldung/web/service/StudentService.java
new file mode 100644
index 0000000000..1b194f76e2
--- /dev/null
+++ b/spring-rest-angular/src/main/java/org/baeldung/web/service/StudentService.java
@@ -0,0 +1,7 @@
+package org.baeldung.web.service;
+
+import org.baeldung.web.entity.Student;
+
+public interface StudentService extends IOperations {
+
+}
diff --git a/spring-rest-angular/src/main/java/org/baeldung/web/service/StudentServiceImpl.java b/spring-rest-angular/src/main/java/org/baeldung/web/service/StudentServiceImpl.java
new file mode 100644
index 0000000000..c7bcdc5bd5
--- /dev/null
+++ b/spring-rest-angular/src/main/java/org/baeldung/web/service/StudentServiceImpl.java
@@ -0,0 +1,21 @@
+package org.baeldung.web.service;
+
+import org.baeldung.web.dao.StudentRepository;
+import org.baeldung.web.entity.Student;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.stereotype.Service;
+
+@Service
+public class StudentServiceImpl implements StudentService {
+
+ @Autowired
+ private StudentRepository dao;
+
+ @Override
+ public Page findPaginated(int page, int size) {
+ return dao.findAll(new PageRequest(page, size));
+ }
+
+}
diff --git a/spring-rest-angular/src/main/resources/application.properties b/spring-rest-angular/src/main/resources/application.properties
new file mode 100644
index 0000000000..e24db89c8f
--- /dev/null
+++ b/spring-rest-angular/src/main/resources/application.properties
@@ -0,0 +1,4 @@
+server.contextPath=/
+spring.h2.console.enabled=true
+logging.level.org.hibernate.SQL=info
+spring.jpa.hibernate.ddl-auto=none
\ No newline at end of file
diff --git a/spring-rest-angular/src/main/resources/db/sql/data.sql b/spring-rest-angular/src/main/resources/db/sql/data.sql
new file mode 100644
index 0000000000..418381a681
--- /dev/null
+++ b/spring-rest-angular/src/main/resources/db/sql/data.sql
@@ -0,0 +1,47 @@
+CREATE TABLE student (
+ id INTEGER PRIMARY KEY,
+ name VARCHAR(30),
+ gender VARCHAR(10),
+ age INTEGER
+);
+
+INSERT INTO student (id,name,gender,age)
+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/WEB-INF/web.xml b/spring-rest-angular/src/main/webapp/WEB-INF/web.xml
similarity index 99%
rename from spring-rest-angular-pagination/src/main/webapp/WEB-INF/web.xml
rename to spring-rest-angular/src/main/webapp/WEB-INF/web.xml
index ff65bd6b96..6adf31503f 100644
--- a/spring-rest-angular-pagination/src/main/webapp/WEB-INF/web.xml
+++ b/spring-rest-angular/src/main/webapp/WEB-INF/web.xml
@@ -3,7 +3,7 @@
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
-
+
index.html
diff --git a/spring-rest-angular/src/main/webapp/index.html b/spring-rest-angular/src/main/webapp/index.html
new file mode 100644
index 0000000000..49e0d6393d
--- /dev/null
+++ b/spring-rest-angular/src/main/webapp/index.html
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/spring-rest-angular-pagination/src/main/webapp/view/app.js b/spring-rest-angular/src/main/webapp/view/app.js
similarity index 95%
rename from spring-rest-angular-pagination/src/main/webapp/view/app.js
rename to spring-rest-angular/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/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/src/test/java/org/baeldung/web/service/StudentServiceTest.java
similarity index 55%
rename from spring-rest-angular-pagination/src/test/java/org/baeldung/web/service/StudentServiceTest.java
rename to spring-rest-angular/src/test/java/org/baeldung/web/service/StudentServiceTest.java
index 19fe77a1fd..1199f15ab3 100644
--- a/spring-rest-angular-pagination/src/test/java/org/baeldung/web/service/StudentServiceTest.java
+++ b/spring-rest-angular/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,58 +24,42 @@ public class StudentServiceTest {
@Test
public void givenRequestForStudents_whenPageIsOne_expectContainsNames() {
- given().params("page", "1", "size", "2").get(ENDPOINT)
- .then()
- .assertThat().body("content.name", hasItems("Bryan", "Ben"));
+ 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)
- .then()
- .assertThat().body("size", equalTo(2));
+ 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)
- .then()
- .assertThat().body("numberOfElements", equalTo(2));
+ 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)
- .then()
- .statusCode(200);
+ given().params("page", "0", "size", "2").get(ENDPOINT).then().statusCode(200);
}
@Test
public void givenRequestForStudents_whenPageOfResourcesAreRetrievedOutOfBounds_thenExpect500() {
- given().params("page", "1000", "size", "2").get(ENDPOINT)
- .then()
- .statusCode(500);
+ given().params("page", "1000", "size", "2").get(ENDPOINT).then().statusCode(500);
}
@Test
public void givenRequestForStudents_whenPageNotValid_thenExpect500() {
- given().params("page", RandomStringUtils.randomNumeric(5), "size", "2").get(ENDPOINT)
- .then()
- .statusCode(500);
+ given().params("page", RandomStringUtils.randomNumeric(5), "size", "2").get(ENDPOINT).then().statusCode(500);
}
@Test
- public void givenRequestForStudents_whenPageIsFive_expectFiveItems() {
- given().params("page", "1", "size", "5").get(ENDPOINT)
- .then()
- .body("content.studentId.max()", equalTo("5"));
+ public void givenRequestForStudents_whenPageSizeIsFive_expectFiveItems() {
+ given().params("page", "0", "size", "5").get(ENDPOINT).then().body("content.size()", is(5));
}
@Test
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
- given().params("page", "1", "size", "2").get(ENDPOINT)
- .then()
- .assertThat().body("first", equalTo(true));
+ given().params("page", "0", "size", "2").get(ENDPOINT).then().assertThat().body("first", equalTo(true));
}
}