diff --git a/spring-boot-rest-2/pom.xml b/spring-boot-rest-2/pom.xml index d74c393f27..b32e1c153d 100644 --- a/spring-boot-rest-2/pom.xml +++ b/spring-boot-rest-2/pom.xml @@ -30,6 +30,15 @@ springfox-boot-starter 3.0.0 + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + runtime + diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/Address.java b/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/Address.java new file mode 100644 index 0000000000..5c005c70f0 --- /dev/null +++ b/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/Address.java @@ -0,0 +1,57 @@ +package com.baeldung.putvspost; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Address { + + private @Id @GeneratedValue Long id; + private String name; + private String city; + private String postalCode; + + Address() { + } + + public Address(String name, String city, String postalCode) { + this.name = name; + this.city = city; + this.postalCode = postalCode; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getPostalCode() { + return postalCode; + } + + public void setPostalCode(String postalCode) { + this.postalCode = postalCode; + } + + @Override + public String toString() { + return "Address [id=" + id + ", name=" + name + ", city=" + city + ", postalCode=" + postalCode + "]"; + } + +} diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/AddressController.java b/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/AddressController.java new file mode 100644 index 0000000000..f989d5c211 --- /dev/null +++ b/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/AddressController.java @@ -0,0 +1,57 @@ +package com.baeldung.putvspost; + +import java.util.List; +import java.util.Optional; + +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class AddressController { + + private final AddressRepository repository; + + AddressController(AddressRepository repository) { + this.repository = repository; + } + + @GetMapping("/addresses") + List
getAllAddresses() { + return repository.findAll(); + } + + @GetMapping("/addresses/{id}") + Optional
getAddressesById(@PathVariable Long id) { + return repository.findById(id); + } + + @PostMapping("/addresses") + Address createNewAddress(@RequestBody Address newAddress) { + return repository.save(newAddress); + } + + @PutMapping("/addresses/{id}") + Address replaceEmployee(@RequestBody Address newAddress, @PathVariable Long id) { + + return repository.findById(id) + .map(address -> { + address.setCity(newAddress.getCity()); + address.setPostalCode(newAddress.getPostalCode()); + return repository.save(address); + }) + .orElseGet(() -> { + return repository.save(newAddress); + }); + } + + @DeleteMapping("/addresses/{id}") + void deleteEmployee(@PathVariable Long id) { + repository.deleteById(id); + } + +} diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/AddressRepository.java b/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/AddressRepository.java new file mode 100644 index 0000000000..415a3c9030 --- /dev/null +++ b/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/AddressRepository.java @@ -0,0 +1,7 @@ +package com.baeldung.putvspost; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AddressRepository extends JpaRepository { + +} diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/PutVsPostApplication.java b/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/PutVsPostApplication.java new file mode 100644 index 0000000000..8cea53d269 --- /dev/null +++ b/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/PutVsPostApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.putvspost; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class PutVsPostApplication { + + public static void main(String[] args) { + SpringApplication.run(PutVsPostApplication.class, args); + } + +}