Skip to content

Commit 5bf7b31

Browse files
author
Ramesh Fadatare
committed
spring boot + spring data jpa + h2 database
1 parent d4361dd commit 5bf7b31

File tree

16 files changed

+624
-0
lines changed

16 files changed

+624
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package net.guides.springboot2.springboot2jpacrudexample;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import org.springframework.http.HttpEntity;
8+
import org.springframework.http.HttpHeaders;
9+
import org.springframework.http.HttpMethod;
10+
import org.springframework.http.MediaType;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.client.RestTemplate;
13+
14+
import net.guides.springboot2.springboot2jpacrudexample.model.Employee;
15+
16+
public class SpringRestClient {
17+
18+
private static final String GET_EMPLOYEES_ENDPOINT_URL = "http://localhost:8080/api/v1/employees";
19+
private static final String GET_EMPLOYEE_ENDPOINT_URL = "http://localhost:8080/api/v1/employees/{id}";
20+
private static final String CREATE_EMPLOYEE_ENDPOINT_URL = "http://localhost:8080/api/v1/employees";
21+
private static final String UPDATE_EMPLOYEE_ENDPOINT_URL = "http://localhost:8080/api/v1/employees/{id}";
22+
private static final String DELETE_EMPLOYEE_ENDPOINT_URL = "http://localhost:8080/api/v1/employees/{id}";
23+
private static RestTemplate restTemplate = new RestTemplate();
24+
25+
public static void main(String[] args) {
26+
SpringRestClient springRestClient = new SpringRestClient();
27+
28+
// Step1: first create a new employee
29+
springRestClient.createEmployee();
30+
31+
// Step 2: get new created employee from step1
32+
springRestClient.getEmployeeById();
33+
34+
// Step3: get all employees
35+
springRestClient.getEmployees();
36+
37+
// Step4: Update employee with id = 1
38+
springRestClient.updateEmployee();
39+
40+
// Step5: Delete employee with id = 1
41+
springRestClient.deleteEmployee();
42+
}
43+
44+
private void getEmployees() {
45+
46+
HttpHeaders headers = new HttpHeaders();
47+
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
48+
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
49+
50+
ResponseEntity<String> result = restTemplate.exchange(GET_EMPLOYEES_ENDPOINT_URL, HttpMethod.GET, entity,
51+
String.class);
52+
53+
System.out.println(result);
54+
}
55+
56+
private void getEmployeeById() {
57+
58+
Map<String, String> params = new HashMap<String, String>();
59+
params.put("id", "1");
60+
61+
RestTemplate restTemplate = new RestTemplate();
62+
Employee result = restTemplate.getForObject(GET_EMPLOYEE_ENDPOINT_URL, Employee.class, params);
63+
64+
System.out.println(result);
65+
}
66+
67+
private void createEmployee() {
68+
69+
Employee newEmployee = new Employee("admin", "admin", "[email protected]");
70+
71+
RestTemplate restTemplate = new RestTemplate();
72+
Employee result = restTemplate.postForObject(CREATE_EMPLOYEE_ENDPOINT_URL, newEmployee, Employee.class);
73+
74+
System.out.println(result);
75+
}
76+
77+
private void updateEmployee() {
78+
Map<String, String> params = new HashMap<String, String>();
79+
params.put("id", "1");
80+
Employee updatedEmployee = new Employee("admin123", "admin123", "[email protected]");
81+
RestTemplate restTemplate = new RestTemplate();
82+
restTemplate.put(UPDATE_EMPLOYEE_ENDPOINT_URL, updatedEmployee, params);
83+
}
84+
85+
private void deleteEmployee() {
86+
Map<String, String> params = new HashMap<String, String>();
87+
params.put("id", "1");
88+
RestTemplate restTemplate = new RestTemplate();
89+
restTemplate.delete(DELETE_EMPLOYEE_ENDPOINT_URL, params);
90+
}
91+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
46.5 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>net.guides.springboot2</groupId>
8+
<artifactId>springboot2-jpa-crud-example</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>springboot2-jpa-crud-example</name>
13+
<description>Demo project for Spring Boot</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>2.0.5.RELEASE</version>
19+
<relativePath /> <!-- lookup parent from repository -->
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25+
<java.version>1.8</java.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-data-jpa</artifactId>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>com.h2database</groupId>
41+
<artifactId>h2</artifactId>
42+
<scope>runtime</scope>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
62+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.guides.springboot2.springboot2jpacrudexample;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.web.client.RestTemplateBuilder;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.web.client.RestTemplate;
8+
9+
@SpringBootApplication
10+
public class Application {
11+
12+
@Bean
13+
public RestTemplate restTemplate(RestTemplateBuilder builder) {
14+
return builder.build();
15+
}
16+
17+
public static void main(String[] args) {
18+
SpringApplication.run(Application.class, args);
19+
}
20+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package net.guides.springboot2.springboot2jpacrudexample;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import org.springframework.http.HttpEntity;
8+
import org.springframework.http.HttpHeaders;
9+
import org.springframework.http.HttpMethod;
10+
import org.springframework.http.MediaType;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.client.RestTemplate;
13+
14+
import net.guides.springboot2.springboot2jpacrudexample.model.Employee;
15+
16+
public class SpringRestClient {
17+
18+
private static final String GET_EMPLOYEES_ENDPOINT_URL = "http://localhost:8080/api/v1/employees";
19+
private static final String GET_EMPLOYEE_ENDPOINT_URL = "http://localhost:8080/api/v1/employees/{id}";
20+
private static final String CREATE_EMPLOYEE_ENDPOINT_URL = "http://localhost:8080/api/v1/employees";
21+
private static final String UPDATE_EMPLOYEE_ENDPOINT_URL = "http://localhost:8080/api/v1/employees/{id}";
22+
private static final String DELETE_EMPLOYEE_ENDPOINT_URL = "http://localhost:8080/api/v1/employees/{id}";
23+
private static RestTemplate restTemplate = new RestTemplate();
24+
25+
public static void main(String[] args) {
26+
SpringRestClient springRestClient = new SpringRestClient();
27+
28+
// Step1: first create a new employee
29+
springRestClient.createEmployee();
30+
31+
// Step 2: get new created employee from step1
32+
springRestClient.getEmployeeById();
33+
34+
// Step3: get all employees
35+
springRestClient.getEmployees();
36+
37+
// Step4: Update employee with id = 1
38+
springRestClient.updateEmployee();
39+
40+
// Step5: Delete employee with id = 1
41+
springRestClient.deleteEmployee();
42+
}
43+
44+
private void getEmployees() {
45+
46+
HttpHeaders headers = new HttpHeaders();
47+
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
48+
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
49+
50+
ResponseEntity<String> result = restTemplate.exchange(GET_EMPLOYEES_ENDPOINT_URL, HttpMethod.GET, entity,
51+
String.class);
52+
53+
System.out.println(result);
54+
}
55+
56+
private void getEmployeeById() {
57+
58+
Map<String, String> params = new HashMap<String, String>();
59+
params.put("id", "1");
60+
61+
RestTemplate restTemplate = new RestTemplate();
62+
Employee result = restTemplate.getForObject(GET_EMPLOYEE_ENDPOINT_URL, Employee.class, params);
63+
64+
System.out.println(result);
65+
}
66+
67+
private void createEmployee() {
68+
69+
Employee newEmployee = new Employee("admin", "admin", "[email protected]");
70+
71+
RestTemplate restTemplate = new RestTemplate();
72+
Employee result = restTemplate.postForObject(CREATE_EMPLOYEE_ENDPOINT_URL, newEmployee, Employee.class);
73+
74+
System.out.println(result);
75+
}
76+
77+
private void updateEmployee() {
78+
Map<String, String> params = new HashMap<String, String>();
79+
params.put("id", "1");
80+
Employee updatedEmployee = new Employee("admin123", "admin123", "[email protected]");
81+
RestTemplate restTemplate = new RestTemplate();
82+
restTemplate.put(UPDATE_EMPLOYEE_ENDPOINT_URL, updatedEmployee, params);
83+
}
84+
85+
private void deleteEmployee() {
86+
Map<String, String> params = new HashMap<String, String>();
87+
params.put("id", "1");
88+
RestTemplate restTemplate = new RestTemplate();
89+
restTemplate.delete(DELETE_EMPLOYEE_ENDPOINT_URL, params);
90+
}
91+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package net.guides.springboot2.springboot2jpacrudexample.controller;
2+
3+
import java.util.HashMap;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import javax.validation.Valid;
9+
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.bind.annotation.CrossOrigin;
13+
import org.springframework.web.bind.annotation.DeleteMapping;
14+
import org.springframework.web.bind.annotation.GetMapping;
15+
import org.springframework.web.bind.annotation.PathVariable;
16+
import org.springframework.web.bind.annotation.PostMapping;
17+
import org.springframework.web.bind.annotation.PutMapping;
18+
import org.springframework.web.bind.annotation.RequestBody;
19+
import org.springframework.web.bind.annotation.RequestMapping;
20+
import org.springframework.web.bind.annotation.RestController;
21+
22+
import net.guides.springboot2.springboot2jpacrudexample.exception.ResourceNotFoundException;
23+
import net.guides.springboot2.springboot2jpacrudexample.model.Employee;
24+
import net.guides.springboot2.springboot2jpacrudexample.repository.EmployeeRepository;
25+
26+
@CrossOrigin(origins = "http://localhost:4200")
27+
@RestController
28+
@RequestMapping("/api/v1")
29+
public class EmployeeController {
30+
@Autowired
31+
private EmployeeRepository employeeRepository;
32+
33+
@GetMapping("/employees")
34+
public List<Employee> getAllEmployees() {
35+
return employeeRepository.findAll();
36+
}
37+
38+
@GetMapping("/employees/{id}")
39+
public ResponseEntity<Employee> getEmployeeById(@PathVariable(value = "id") Long employeeId)
40+
throws ResourceNotFoundException {
41+
Employee employee = employeeRepository.findById(employeeId)
42+
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
43+
return ResponseEntity.ok().body(employee);
44+
}
45+
46+
@PostMapping("/employees")
47+
public Employee createEmployee(@Valid @RequestBody Employee employee) {
48+
return employeeRepository.save(employee);
49+
}
50+
51+
@PutMapping("/employees/{id}")
52+
public ResponseEntity<Employee> updateEmployee(@PathVariable(value = "id") Long employeeId,
53+
@Valid @RequestBody Employee employeeDetails) throws ResourceNotFoundException {
54+
Employee employee = employeeRepository.findById(employeeId)
55+
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
56+
57+
employee.setEmailId(employeeDetails.getEmailId());
58+
employee.setLastName(employeeDetails.getLastName());
59+
employee.setFirstName(employeeDetails.getFirstName());
60+
final Employee updatedEmployee = employeeRepository.save(employee);
61+
return ResponseEntity.ok(updatedEmployee);
62+
}
63+
64+
@DeleteMapping("/employees/{id}")
65+
public Map<String, Boolean> deleteEmployee(@PathVariable(value = "id") Long employeeId)
66+
throws ResourceNotFoundException {
67+
Employee employee = employeeRepository.findById(employeeId)
68+
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
69+
70+
employeeRepository.delete(employee);
71+
Map<String, Boolean> response = new HashMap<>();
72+
response.put("deleted", Boolean.TRUE);
73+
return response;
74+
}
75+
}

0 commit comments

Comments
 (0)