Merge pull request #54 from mayank4all/spring-boot-rest-template
Adding the Spring Boot Rest template project
This commit is contained in:
33
Spring-Boot/spring-boot-rest-template/.gitignore
vendored
Normal file
33
Spring-Boot/spring-boot-rest-template/.gitignore
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
BIN
Spring-Boot/spring-boot-rest-template/.mvn/wrapper/maven-wrapper.jar
vendored
Normal file
BIN
Spring-Boot/spring-boot-rest-template/.mvn/wrapper/maven-wrapper.jar
vendored
Normal file
Binary file not shown.
2
Spring-Boot/spring-boot-rest-template/.mvn/wrapper/maven-wrapper.properties
vendored
Normal file
2
Spring-Boot/spring-boot-rest-template/.mvn/wrapper/maven-wrapper.properties
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.4/apache-maven-3.8.4-bin.zip
|
||||
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
|
||||
63
Spring-Boot/spring-boot-rest-template/pom.xml
Normal file
63
Spring-Boot/spring-boot-rest-template/pom.xml
Normal file
@@ -0,0 +1,63 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.6.3</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.javadevjournal</groupId>
|
||||
<artifactId>spring-boot-rest-template</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-boot-rest-template</name>
|
||||
<description>spring-boot-rest-template</description>
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.javadevjournal.springbootresttemplate;
|
||||
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootRestTemplateApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootRestTemplateApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@Bean("restTemplateWithTimeout")
|
||||
RestTemplate restTemplateWithTimeout() {
|
||||
return new RestTemplate(getClientHttpRequestFactory());
|
||||
}
|
||||
|
||||
ClientHttpRequestFactory getClientHttpRequestFactory() {
|
||||
int timeout = 5000;
|
||||
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory
|
||||
= new HttpComponentsClientHttpRequestFactory();
|
||||
clientHttpRequestFactory.setConnectTimeout(timeout);
|
||||
return clientHttpRequestFactory;
|
||||
}
|
||||
|
||||
ClientHttpRequestFactory getClientHttpRequestFactoryV1() {
|
||||
int timeout = 5000;
|
||||
RequestConfig config = RequestConfig.custom()
|
||||
.setConnectTimeout(timeout)
|
||||
.setConnectionRequestTimeout(timeout)
|
||||
.setSocketTimeout(timeout)
|
||||
.build();
|
||||
CloseableHttpClient client = HttpClientBuilder
|
||||
.create()
|
||||
.setDefaultRequestConfig(config)
|
||||
.build();
|
||||
return new HttpComponentsClientHttpRequestFactory(client);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.javadevjournal.springbootresttemplate.controller;
|
||||
|
||||
import com.javadevjournal.springbootresttemplate.model.Employee;
|
||||
import com.javadevjournal.springbootresttemplate.service.EmployeeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class EmployeeController {
|
||||
|
||||
@Autowired
|
||||
EmployeeService employeeService;
|
||||
|
||||
@GetMapping("/employees")
|
||||
private List getAllEmployees() {
|
||||
return employeeService.getAllEmployees();
|
||||
}
|
||||
|
||||
@GetMapping("/employees/{id}")
|
||||
private Employee getEmployeeById(@PathVariable("id") int id) {
|
||||
return employeeService.getEmployeeById(id);
|
||||
}
|
||||
|
||||
@PostMapping("/employees")
|
||||
private Employee createEmployee(@RequestBody Employee employee) {
|
||||
employeeService.saveOrUpdate(employee);
|
||||
return employee;
|
||||
}
|
||||
|
||||
@PutMapping("/employees/{id}")
|
||||
private Employee updateEmployee(@PathVariable("id") int id, @RequestBody Employee employee) {
|
||||
Employee updatedEmployee = employeeService.getEmployeeById(id);
|
||||
updatedEmployee.setName(employee.getName());
|
||||
updatedEmployee.setSalary(employee.getSalary());
|
||||
employeeService.saveOrUpdate(updatedEmployee);
|
||||
return updatedEmployee;
|
||||
}
|
||||
|
||||
@DeleteMapping("/employees/{id}")
|
||||
private Employee deleteById(@PathVariable("id") int id) {
|
||||
Employee employeeDeleted = employeeService.getEmployeeById(id);
|
||||
employeeService.delete(id);
|
||||
return employeeDeleted;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package com.javadevjournal.springbootresttemplate.controller;
|
||||
|
||||
import com.javadevjournal.springbootresttemplate.model.Employee;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import java.util.*;
|
||||
|
||||
@RestController
|
||||
public class RestTemplateController {
|
||||
private final String URI_EMPLOYEE = "http://localhost:8081/employees/";
|
||||
private final String URI_EMPLOYEE_ID = "http://localhost:8081/employees/{id}";
|
||||
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
|
||||
@GetMapping("/v1/allEmployees")
|
||||
public ResponseEntity getAllV1() {
|
||||
Employee[] EmployeesArray = restTemplate.getForObject(URI_EMPLOYEE, Employee[].class);
|
||||
return new ResponseEntity<>(Arrays.asList(EmployeesArray), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/v1/employees/{id}")
|
||||
public ResponseEntity getByIdV1(@PathVariable final Integer id) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("id", String.valueOf(id));
|
||||
Employee Employee = restTemplate.getForObject(URI_EMPLOYEE_ID, Employee.class, params);
|
||||
return new ResponseEntity<>(Employee, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/v2/allEmployees")
|
||||
public ResponseEntity getAllV2() {
|
||||
ResponseEntity<Employee[]> responseEntity = restTemplate.getForEntity(URI_EMPLOYEE, Employee[].class);
|
||||
return responseEntity;
|
||||
}
|
||||
|
||||
@GetMapping("/v2/employees/{id}")
|
||||
public ResponseEntity getByIdV2(@PathVariable final Integer id) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("id", String.valueOf(id));
|
||||
ResponseEntity<Employee> responseEntity
|
||||
= restTemplate.getForEntity(URI_EMPLOYEE_ID, Employee.class, params);
|
||||
return responseEntity;
|
||||
}
|
||||
|
||||
@GetMapping("/v3/allEmployees")
|
||||
public ResponseEntity getAllV3() {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
HttpEntity<String> entity = new HttpEntity<>(httpHeaders);
|
||||
return restTemplate.exchange(URI_EMPLOYEE, HttpMethod.GET, entity, Employee[].class);
|
||||
}
|
||||
|
||||
@GetMapping("/v3/employees/{id}")
|
||||
public ResponseEntity getByIdV3(@PathVariable final Integer id) {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
HttpEntity<String> entity = new HttpEntity<>(httpHeaders);
|
||||
return restTemplate.exchange(URI_EMPLOYEE + id, HttpMethod.GET, entity, Employee.class);
|
||||
}
|
||||
|
||||
@PostMapping("/v1/employees")
|
||||
public ResponseEntity createV1(@RequestBody final Employee newEmployee) {
|
||||
Employee createdEmployee = restTemplate.postForObject(URI_EMPLOYEE, newEmployee, Employee.class);
|
||||
return new ResponseEntity(createdEmployee, HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@PostMapping("/v2/employees")
|
||||
public ResponseEntity createV2(@RequestBody final Employee newEmployee) {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
HttpEntity<Employee> entity = new HttpEntity<>(newEmployee, httpHeaders);
|
||||
return restTemplate.exchange(URI_EMPLOYEE, HttpMethod.POST, entity, Employee.class);
|
||||
}
|
||||
|
||||
@PutMapping("/v1/employees/{id}")
|
||||
public ResponseEntity updateEmployeeV1(@PathVariable final Integer id, @RequestBody Employee newEmployee) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("id", String.valueOf(id));
|
||||
restTemplate.put(URI_EMPLOYEE_ID, newEmployee, params);
|
||||
return new ResponseEntity("Employee Updated with id " + id, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PutMapping("/v2/employees/{id}")
|
||||
public ResponseEntity updateEmployeeV2(@PathVariable final Integer id, @RequestBody Employee newEmployee) {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
HttpEntity<Employee> entity = new HttpEntity<>(newEmployee, httpHeaders);
|
||||
return restTemplate.exchange(URI_EMPLOYEE + id, HttpMethod.PUT, entity, Employee.class);
|
||||
}
|
||||
|
||||
@DeleteMapping("/v1/employees/{id}")
|
||||
public ResponseEntity deleteV1(@PathVariable final Integer id) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("id", String.valueOf(id));
|
||||
restTemplate.delete(URI_EMPLOYEE_ID, params);
|
||||
return new ResponseEntity<>("Employee deleted with id " + id, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@DeleteMapping("/v2/employees/{id}")
|
||||
public ResponseEntity<Employee> deleteV2(@PathVariable final Integer id) {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
HttpEntity<Employee> entity = new HttpEntity<>(httpHeaders);
|
||||
return restTemplate.exchange(URI_EMPLOYEE + id, HttpMethod.DELETE, entity, Employee.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.javadevjournal.springbootresttemplate.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
|
||||
@Entity
|
||||
@Table(name = "EMPLOYEE")
|
||||
public class Employee implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
Integer id;
|
||||
String name;
|
||||
Double salary;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(Double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.javadevjournal.springbootresttemplate.repository;
|
||||
|
||||
import com.javadevjournal.springbootresttemplate.model.Employee;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.javadevjournal.springbootresttemplate.service;
|
||||
|
||||
import com.javadevjournal.springbootresttemplate.model.Employee;
|
||||
import com.javadevjournal.springbootresttemplate.repository.EmployeeRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class EmployeeService {
|
||||
|
||||
@Autowired
|
||||
EmployeeRepository employeeRepository;
|
||||
|
||||
public List getAllEmployees() {
|
||||
List employees = new ArrayList();
|
||||
employeeRepository.findAll().forEach(employee -> employees.add(employee));
|
||||
return employees;
|
||||
}
|
||||
|
||||
public Employee getEmployeeById(int id) {
|
||||
return employeeRepository.findById(id).get();
|
||||
}
|
||||
|
||||
public void saveOrUpdate(Employee employee) {
|
||||
employeeRepository.save(employee);
|
||||
}
|
||||
|
||||
public void delete(int id) {
|
||||
employeeRepository.deleteById(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.javadevjournal.springbootresttemplate;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringBootRestTemplateApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user