Update for Spring Boot 2.0

This commit is contained in:
Greg Turnquist
2017-11-24 22:18:49 -06:00
parent a76e708613
commit a8306629cc
17 changed files with 138 additions and 58 deletions

View File

@@ -20,6 +20,8 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Optional;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@@ -74,6 +76,10 @@ class Employee implements Identifiable<Long> {
this.role = role;
}
public Optional<Long> getId() {
return Optional.ofNullable(this.id);
}
/**
* This method will create another piece of data in the REST resource representation. These types
* of methods are key in supporting backward compatibility.

View File

@@ -70,8 +70,11 @@ class EmployeeController {
*/
@GetMapping(value = "/employees/{id}", produces = MediaTypes.HAL_JSON_VALUE)
public ResponseEntity<Resource<Employee>> findOne(@PathVariable long id) {
return ResponseEntity.ok(
assembler.toResource(repository.findOne(id)));
return repository.findById(id)
.map(assembler::toResource)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
}