HTTP PUT vs HTTP PATCH in a REST API.
This commit is contained in:
25
Spring-Boot/rest-example/.gitignore
vendored
Normal file
25
Spring-Boot/rest-example/.gitignore
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
/target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
58
Spring-Boot/rest-example/pom.xml
Normal file
58
Spring-Boot/rest-example/pom.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.javadevjournal</groupId>
|
||||
<artifactId>rest-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>rest-example</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.3.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.javadevjournal.restexample;
|
||||
|
||||
import com.javadevjournal.restexample.data.Customer;
|
||||
import com.javadevjournal.restexample.jpa.CustomerRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class AppConfig {
|
||||
|
||||
@Autowired
|
||||
private CustomerRepository customerRepository;
|
||||
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
|
||||
customerRepository.save(new Customer("Shekher", "Kumar","abc@email.com"));
|
||||
customerRepository.save(new Customer("Robert", "Hickle","abc1@email.com"));
|
||||
customerRepository.save(new Customer("David", "Palmer","abc3@email.com"));
|
||||
customerRepository.save(new Customer("Michelle", "Dessler", "abc@email.com"));
|
||||
|
||||
Iterable<Customer> custmomer = customerRepository.findAll();
|
||||
System.out.println("*******************************");
|
||||
custmomer.forEach((Customer cust)->{
|
||||
System.out.println(cust.getId());
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.javadevjournal.restexample;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class RestExampleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RestExampleApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.javadevjournal.restexample.controller;
|
||||
|
||||
import com.javadevjournal.restexample.data.Customer;
|
||||
import com.javadevjournal.restexample.jpa.CustomerRepository;
|
||||
import com.javadevjournal.restexample.service.CustomerService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
public class RESTController {
|
||||
|
||||
@Autowired
|
||||
private CustomerService customerService;
|
||||
|
||||
@PutMapping("/customers/{id}")
|
||||
public ResponseEntity<?> saveResource(@RequestBody Customer customer,
|
||||
@PathVariable("id") String id) {
|
||||
Customer newCustomer = customerService.saveCustomer(customer,id);
|
||||
return new ResponseEntity<>(newCustomer,HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PatchMapping("/customers/{id}")
|
||||
public ResponseEntity<?> updateResource(@RequestParam("email") String email, @PathVariable("id") String id){
|
||||
Customer newCustomer = customerService.updateCustomer(email,id);
|
||||
return new ResponseEntity<>(newCustomer,HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.javadevjournal.restexample.data;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "customer")
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private Long id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String email;
|
||||
|
||||
public Customer() {
|
||||
}
|
||||
|
||||
public Customer(String firstName, String lastName, String email) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.javadevjournal.restexample.jpa;
|
||||
|
||||
import com.javadevjournal.restexample.data.Customer;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface CustomerRepository extends CrudRepository<Customer,Long> {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.javadevjournal.restexample.service;
|
||||
|
||||
import com.javadevjournal.restexample.data.Customer;
|
||||
|
||||
public interface CustomerService {
|
||||
|
||||
public Customer saveCustomer(Customer customer, String id);
|
||||
public Customer updateCustomer(String email, String id);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.javadevjournal.restexample.service;
|
||||
|
||||
import com.javadevjournal.restexample.data.Customer;
|
||||
import com.javadevjournal.restexample.jpa.CustomerRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class DefaultCustomerService implements CustomerService {
|
||||
|
||||
@Autowired
|
||||
CustomerRepository customerRepository;
|
||||
|
||||
@Override
|
||||
public Customer saveCustomer(Customer customer, String id) {
|
||||
|
||||
Optional<Customer> originalCustomer = customerRepository.findById(Long.valueOf(id));
|
||||
return customerRepository.save(customer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer updateCustomer(String email, String id) {
|
||||
Optional<Customer> originalCustomer = customerRepository.findById(Long.valueOf(id));
|
||||
Customer customer = originalCustomer.get();
|
||||
customer.setEmail(email);
|
||||
return customerRepository.save(customer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.javadevjournal.restexample;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class RestExampleApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user