Merge pull request #608 from jesus-dayo/master
spring rest angular changes based on feedback, added also new tutorial for mapstruct
This commit is contained in:
commit
2fb54f3aeb
|
@ -0,0 +1,49 @@
|
|||
<?xml version="1.0"?>
|
||||
<project
|
||||
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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<name>mapstruct</name>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<org.mapstruct.version>1.0.0.Final</org.mapstruct.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-jdk8</artifactId>
|
||||
<version>${org.mapstruct.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>mapstruct</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-processor</artifactId>
|
||||
<version>${org.mapstruct.version}</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Student> 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<Student> getMockDataStudents(){
|
||||
return studentList;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Student> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size){
|
||||
|
||||
Page<Student> resultPage = service.findPaginated(page, size);
|
||||
|
||||
return resultPage;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package org.baeldung.web.service;
|
||||
|
||||
import org.baeldung.web.vo.Student;
|
||||
|
||||
public interface StudentService extends IOperations<Student>{
|
||||
|
||||
}
|
|
@ -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<Student> mockDataStudent = MockStudentData.getMockDataStudents();
|
||||
|
||||
@Override
|
||||
public Page<Student> findPaginated(int page, int size){
|
||||
Page<Student> studentPage = getPage(page, size);
|
||||
return studentPage;
|
||||
}
|
||||
|
||||
private Page<Student> 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<Student>(mockDataStudent.subList(from, to),
|
||||
new PageRequest(page,size),
|
||||
mockDataStudent.size());
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
server.contextPath=/
|
|
@ -1,14 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en" ng-app="app">
|
||||
<head>
|
||||
<link rel="stylesheet" href="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.css">
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
|
||||
<script src="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js"></script>
|
||||
<script src="view/app.js"></script>
|
||||
</head>
|
||||
<body >
|
||||
<div ng-controller="StudentCtrl as vm">
|
||||
<div ui-grid="gridOptions" class="grid" ui-grid-pagination></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>angular-spring-rest-sample</artifactId>
|
||||
<name>angular-spring-rest-sample</name>
|
||||
<artifactId>spring-rest-angular</artifactId>
|
||||
<name>spring-rest-angular</name>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0</version>
|
||||
<packaging>war</packaging>
|
||||
|
@ -13,9 +13,6 @@
|
|||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.3.RELEASE</version>
|
||||
</parent>
|
||||
<properties>
|
||||
<spring.data.version>1.12.2.RELEASE</spring.data.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -30,6 +27,15 @@
|
|||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
|
@ -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<Student, Long> {
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Student> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size) {
|
||||
|
||||
Page<Student> resultPage = service.findPaginated(page, size);
|
||||
if (page > resultPage.getTotalPages()) {
|
||||
throw new MyResourceNotFoundException();
|
||||
}
|
||||
return resultPage;
|
||||
}
|
||||
|
||||
}
|
|
@ -4,6 +4,6 @@ import org.springframework.data.domain.Page;
|
|||
|
||||
public interface IOperations<T> {
|
||||
|
||||
Page<T> findPaginated(int page, int size);
|
||||
public Page<T> findPaginated(final int page, final int size);
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package org.baeldung.web.service;
|
||||
|
||||
import org.baeldung.web.entity.Student;
|
||||
|
||||
public interface StudentService extends IOperations<Student> {
|
||||
|
||||
}
|
|
@ -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<Student> findPaginated(int page, int size) {
|
||||
return dao.findAll(new PageRequest(page, size));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
server.contextPath=/
|
||||
spring.h2.console.enabled=true
|
||||
logging.level.org.hibernate.SQL=info
|
||||
spring.jpa.hibernate.ddl-auto=none
|
|
@ -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);
|
|
@ -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">
|
||||
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.html</welcome-file>
|
||||
</welcome-file-list>
|
|
@ -0,0 +1,17 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en" ng-app="app">
|
||||
<head>
|
||||
<link rel="stylesheet"
|
||||
href="//cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.css">
|
||||
<script
|
||||
src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
|
||||
<script
|
||||
src="//cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js"></script>
|
||||
<script src="view/app.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div ng-controller="StudentCtrl as vm">
|
||||
<div ui-grid="gridOptions" class="grid" ui-grid-pagination></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -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
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue