Merge branch 'eugenp:master' into master
This commit is contained in:
commit
6442bc8e47
@ -1,3 +1,4 @@
|
|||||||
### Relevant Article:
|
### Relevant Article:
|
||||||
|
|
||||||
- [Get All Endpoints in Spring Boot](https://www.baeldung.com/spring-boot-get-all-endpoints)
|
- [Get All Endpoints in Spring Boot](https://www.baeldung.com/spring-boot-get-all-endpoints)
|
||||||
|
- [HTTP PUT vs. POST in REST API](https://www.baeldung.com/rest-http-put-vs-post)
|
||||||
|
@ -30,6 +30,15 @@
|
|||||||
<artifactId>springfox-boot-starter</artifactId>
|
<artifactId>springfox-boot-starter</artifactId>
|
||||||
<version>3.0.0</version>
|
<version>3.0.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -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 + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<Address> getAllAddresses() {
|
||||||
|
return repository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/addresses/{id}")
|
||||||
|
Optional<Address> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.putvspost;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface AddressRepository extends JpaRepository<Address, Long> {
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user