Polishing.
This commit is contained in:
@@ -33,13 +33,12 @@ import javax.persistence.Id;
|
||||
@Entity
|
||||
class Employee {
|
||||
|
||||
@Id @GeneratedValue
|
||||
private Long id;
|
||||
@Id @GeneratedValue private Long id;
|
||||
private String name;
|
||||
private String role;
|
||||
|
||||
Employee(String name, String role) {
|
||||
|
||||
|
||||
this.name = name;
|
||||
this.role = 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;
|
||||
@@ -47,9 +47,9 @@ class EmployeeController {
|
||||
|
||||
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,18 +64,18 @@ class EmployeeController {
|
||||
|
||||
Employee savedEmployee = repository.save(employee);
|
||||
|
||||
return ResponseEntity
|
||||
.created(savedEmployee.getId()
|
||||
.map(id -> linkTo(methodOn(EmployeeController.class).findOne(id)).toUri())
|
||||
.orElseThrow(() -> new RuntimeException("Failed to create for some reason")))
|
||||
.body(assembler.toModel(savedEmployee));
|
||||
return ResponseEntity //
|
||||
.created(savedEmployee.getId() //
|
||||
.map(id -> linkTo(methodOn(EmployeeController.class).findOne(id)).toUri()) //
|
||||
.orElseThrow(() -> new RuntimeException("Failed to create for some reason"))) //
|
||||
.body(assembler.toModel(savedEmployee));
|
||||
}
|
||||
|
||||
@GetMapping("/employees/{id}")
|
||||
public EntityModel<Employee> findOne(@PathVariable Long id) {
|
||||
return repository.findById(id)
|
||||
.map(assembler::toModel)
|
||||
.orElseThrow(() -> new RuntimeException("No employee '" + id + "' found"));
|
||||
return repository.findById(id) //
|
||||
.map(assembler::toModel) //
|
||||
.orElseThrow(() -> new RuntimeException("No employee '" + id + "' found"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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", "ring bearer"));
|
||||
repository.save(new Employee("Bilbo", "burglar"));
|
||||
|
||||
Reference in New Issue
Block a user