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
This commit is contained in:
Mainak Majumder 2021-05-15 04:44:22 +02:00 committed by GitHub
parent 3c5b7234d9
commit b7bf539a69
5 changed files with 143 additions and 0 deletions

View File

@ -30,6 +30,15 @@
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</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>
<build>

View File

@ -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 + "]";
}
}

View File

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

View File

@ -0,0 +1,7 @@
package com.baeldung.putvspost;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AddressRepository extends JpaRepository<Address, Long> {
}

View File

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