Polishing.
This commit is contained in:
@@ -28,8 +28,8 @@ import javax.persistence.Id;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* An updated domain object where {@literal name} has been replaced by {@literal firstName} and {@literal} lastName.
|
||||
* To easy migration, we need to support the old {@literal name} field with a getter and a setter.
|
||||
* An updated domain object where {@literal name} has been replaced by {@literal firstName} and {@literal} lastName. To
|
||||
* easy migration, we need to support the old {@literal name} field with a getter and a setter.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
@@ -38,8 +38,7 @@ import org.springframework.util.StringUtils;
|
||||
@Entity
|
||||
class Employee {
|
||||
|
||||
@Id @GeneratedValue
|
||||
private Long id;
|
||||
@Id @GeneratedValue private Long id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String role;
|
||||
|
||||
@@ -17,9 +17,9 @@ package org.springframework.hateoas.examples;
|
||||
|
||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
|
||||
|
||||
import org.springframework.hateoas.CollectionModel;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.hateoas.RepresentationModel;
|
||||
import org.springframework.hateoas.CollectionModel;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@@ -46,10 +46,10 @@ class EmployeeController {
|
||||
public RepresentationModel root() {
|
||||
|
||||
RepresentationModel rootResource = new RepresentationModel();
|
||||
|
||||
rootResource.add(
|
||||
linkTo(methodOn(EmployeeController.class).root()).withSelfRel(),
|
||||
linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees"));
|
||||
|
||||
rootResource.add( //
|
||||
linkTo(methodOn(EmployeeController.class).root()).withSelfRel(), //
|
||||
linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees"));
|
||||
|
||||
return rootResource;
|
||||
}
|
||||
@@ -64,20 +64,19 @@ class EmployeeController {
|
||||
|
||||
Employee savedEmployee = repository.save(employee);
|
||||
|
||||
return savedEmployee.getId()
|
||||
.map(id -> ResponseEntity
|
||||
.created(linkTo(methodOn(EmployeeController.class).findOne(id)).toUri())
|
||||
.body(assembler.toModel(savedEmployee)))
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
return savedEmployee.getId() //
|
||||
.map(id -> ResponseEntity.created( //
|
||||
linkTo(methodOn(EmployeeController.class).findOne(id)).toUri()).body(assembler.toModel(savedEmployee)))
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@GetMapping("/employees/{id}")
|
||||
public ResponseEntity<EntityModel<Employee>> findOne(@PathVariable Long id) {
|
||||
|
||||
return repository.findById(id)
|
||||
.map(assembler::toModel)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
return repository.findById(id) //
|
||||
.map(assembler::toModel) //
|
||||
.map(ResponseEntity::ok) //
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.hateoas.examples;/*
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -13,6 +13,7 @@ package org.springframework.hateoas.examples;/*
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.hateoas.examples;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ class InitDatabase {
|
||||
|
||||
@Bean
|
||||
CommandLineRunner loadEmployees() {
|
||||
|
||||
return args -> {
|
||||
repository.save(new Employee("Frodo", "Baggins", "ring bearer"));
|
||||
repository.save(new Employee("Bilbo", "Baggins", "burglar"));
|
||||
|
||||
Reference in New Issue
Block a user