Upgrade to latest Spring HATEOAS API changes.

This commit is contained in:
Greg Turnquist
2019-03-03 10:52:08 -06:00
parent 35c74de6cb
commit 15db5a0513
28 changed files with 185 additions and 180 deletions

View File

@@ -25,7 +25,6 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.springframework.hateoas.Identifiable;
import org.springframework.util.StringUtils;
/**
@@ -37,7 +36,7 @@ import org.springframework.util.StringUtils;
@Data
@NoArgsConstructor
@Entity
class Employee implements Identifiable<Long> {
class Employee {
@Id @GeneratedValue
private Long id;

View File

@@ -15,11 +15,11 @@
*/
package org.springframework.hateoas.examples;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.hateoas.Resources;
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;
@@ -43,9 +43,9 @@ class EmployeeController {
}
@GetMapping("/")
public ResourceSupport root() {
public RepresentationModel root() {
ResourceSupport rootResource = new ResourceSupport();
RepresentationModel rootResource = new RepresentationModel();
rootResource.add(
linkTo(methodOn(EmployeeController.class).root()).withSelfRel(),
@@ -55,27 +55,27 @@ class EmployeeController {
}
@GetMapping("/employees")
public Resources<Resource<Employee>> findAll() {
return assembler.toResources(repository.findAll());
public CollectionModel<EntityModel<Employee>> findAll() {
return assembler.toCollectionModel(repository.findAll());
}
@PostMapping("/employees")
public ResponseEntity<Resource<Employee>> newEmployee(@RequestBody Employee employee) {
public ResponseEntity<EntityModel<Employee>> newEmployee(@RequestBody Employee employee) {
Employee savedEmployee = repository.save(employee);
return savedEmployee.getId()
.map(id -> ResponseEntity
.created(linkTo(methodOn(EmployeeController.class).findOne(id)).toUri())
.body(assembler.toResource(savedEmployee)))
.body(assembler.toModel(savedEmployee)))
.orElse(ResponseEntity.notFound().build());
}
@GetMapping("/employees/{id}")
public ResponseEntity<Resource<Employee>> findOne(@PathVariable Long id) {
public ResponseEntity<EntityModel<Employee>> findOne(@PathVariable Long id) {
return repository.findById(id)
.map(assembler::toResource)
.map(assembler::toModel)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}

View File

@@ -15,14 +15,14 @@
*/
package org.springframework.hateoas.examples;
import org.springframework.hateoas.SimpleIdentifiableResourceAssembler;
import org.springframework.hateoas.SimpleIdentifiableRepresentationModelAssembler;
import org.springframework.stereotype.Component;
/**
* @author Greg Turnquist
*/
@Component
class EmployeeResourceAssembler extends SimpleIdentifiableResourceAssembler<Employee> {
class EmployeeResourceAssembler extends SimpleIdentifiableRepresentationModelAssembler<Employee> {
EmployeeResourceAssembler() {
super(EmployeeController.class);