Mainak Majumder b7bf539a69 Code for the Baeldung article "HTTP PUT vs POST in REST (#10707)
* Application source code for the Baeldung article "HTTP PUT vs POST
method in REST API"

* update indention in pom file, update code in Address class

* update indention

* rename application

* update pom
2021-05-14 21:44:22 -05:00

58 lines
1.7 KiB
Java

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);
}
}