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,8 +25,6 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import org.springframework.hateoas.Identifiable;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
@@ -35,7 +33,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
@Data
@Entity
@NoArgsConstructor
class Employee implements Identifiable<Long> {
class Employee {
@Id @GeneratedValue
private Long id;

View File

@@ -18,8 +18,8 @@ package org.springframework.hateoas.examples;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.CollectionModel;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -32,10 +32,10 @@ import org.springframework.web.bind.annotation.RestController;
class EmployeeController {
private final EmployeeRepository repository;
private final EmployeeResourceAssembler assembler;
private final EmployeeRepresentationModelAssembler assembler;
private final EmployeeWithManagerResourceAssembler employeeWithManagerResourceAssembler;
EmployeeController(EmployeeRepository repository, EmployeeResourceAssembler assembler,
EmployeeController(EmployeeRepository repository, EmployeeRepresentationModelAssembler assembler,
EmployeeWithManagerResourceAssembler employeeWithManagerResourceAssembler) {
this.repository = repository;
@@ -45,28 +45,28 @@ class EmployeeController {
/**
* Look up all employees, and transform them into a REST collection resource using
* {@link EmployeeResourceAssembler#toResources(Iterable)}. Then return them through
* {@link EmployeeRepresentationModelAssembler#toCollectionModel(Iterable)}. Then return them through
* Spring Web's {@link ResponseEntity} fluent API.
*/
@GetMapping("/employees")
public ResponseEntity<Resources<Resource<Employee>>> findAll() {
public ResponseEntity<CollectionModel<EntityModel<Employee>>> findAll() {
return ResponseEntity.ok(
assembler.toResources(repository.findAll()));
assembler.toCollectionModel(repository.findAll()));
}
/**
* Look up a single {@link Employee} and transform it into a REST resource using
* {@link EmployeeResourceAssembler#toResource(Object)}. Then return it through
* {@link EmployeeRepresentationModelAssembler#toModel(Object)}. Then return it through
* Spring Web's {@link ResponseEntity} fluent API.
*
* @param id
*/
@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());
}
@@ -78,27 +78,27 @@ class EmployeeController {
* @return
*/
@GetMapping("/managers/{id}/employees")
public ResponseEntity<Resources<Resource<Employee>>> findEmployees(@PathVariable long id) {
public ResponseEntity<CollectionModel<EntityModel<Employee>>> findEmployees(@PathVariable long id) {
return ResponseEntity.ok(
assembler.toResources(repository.findByManagerId(id)));
assembler.toCollectionModel(repository.findByManagerId(id)));
}
@GetMapping("/employees/detailed")
public ResponseEntity<Resources<Resource<EmployeeWithManager>>> findAllDetailedEmployees() {
public ResponseEntity<CollectionModel<EntityModel<EmployeeWithManager>>> findAllDetailedEmployees() {
return ResponseEntity.ok(
employeeWithManagerResourceAssembler.toResources(
employeeWithManagerResourceAssembler.toCollectionModel(
StreamSupport.stream(repository.findAll().spliterator(), false)
.map(EmployeeWithManager::new)
.collect(Collectors.toList())));
}
@GetMapping("/employees/{id}/detailed")
public ResponseEntity<Resource<EmployeeWithManager>> findDetailedEmployee(@PathVariable Long id) {
public ResponseEntity<EntityModel<EmployeeWithManager>> findDetailedEmployee(@PathVariable Long id) {
return repository.findById(id)
.map(EmployeeWithManager::new)
.map(employeeWithManagerResourceAssembler::toResource)
.map(employeeWithManagerResourceAssembler::toModel)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}

View File

@@ -15,30 +15,30 @@
*/
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.Resources;
import org.springframework.hateoas.SimpleIdentifiableResourceAssembler;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.SimpleIdentifiableRepresentationModelAssembler;
import org.springframework.stereotype.Component;
/**
* @author Greg Turnquist
*/
@Component
class EmployeeResourceAssembler extends SimpleIdentifiableResourceAssembler<Employee> {
class EmployeeRepresentationModelAssembler extends SimpleIdentifiableRepresentationModelAssembler<Employee> {
EmployeeResourceAssembler() {
EmployeeRepresentationModelAssembler() {
super(EmployeeController.class);
}
/**
* Define links to add to every {@link Resource}.
* Define links to add to every {@link EntityModel}.
*
* @param resource
*/
@Override
public void addLinks(Resource<Employee> resource) {
public void addLinks(EntityModel<Employee> resource) {
/**
* Add some custom links to the default ones provided.
@@ -59,12 +59,12 @@ class EmployeeResourceAssembler extends SimpleIdentifiableResourceAssembler<Empl
}
/**
* Define links to add to {@link Resources} collection.
* Define links to add to {@link CollectionModel} collection.
*
* @param resources
*/
@Override
public void addLinks(Resources<Resource<Employee>> resources) {
public void addLinks(CollectionModel<EntityModel<Employee>> resources) {
super.addLinks(resources);

View File

@@ -15,26 +15,26 @@
*/
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.Resources;
import org.springframework.hateoas.SimpleResourceAssembler;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.SimpleRepresentationModelAssembler;
import org.springframework.stereotype.Component;
/**
* @author Greg Turnquist
*/
@Component
class EmployeeWithManagerResourceAssembler implements SimpleResourceAssembler<EmployeeWithManager> {
class EmployeeWithManagerResourceAssembler implements SimpleRepresentationModelAssembler<EmployeeWithManager> {
/**
* Define links to add to every individual {@link Resource}.
* Define links to add to every individual {@link EntityModel}.
*
* @param resource
*/
@Override
public void addLinks(Resource<EmployeeWithManager> resource) {
public void addLinks(EntityModel<EmployeeWithManager> resource) {
resource.add(linkTo(methodOn(EmployeeController.class).findDetailedEmployee(resource.getContent().getId())).withSelfRel());
resource.add(linkTo(methodOn(EmployeeController.class).findOne(resource.getContent().getId())).withRel("summary"));
@@ -42,12 +42,12 @@ class EmployeeWithManagerResourceAssembler implements SimpleResourceAssembler<Em
}
/**
* Define links to add to the {@link Resources} collection.
* Define links to add to the {@link CollectionModel} collection.
*
* @param resources
*/
@Override
public void addLinks(Resources<Resource<EmployeeWithManager>> resources) {
public void addLinks(CollectionModel<EntityModel<EmployeeWithManager>> resources) {
resources.add(linkTo(methodOn(EmployeeController.class).findAllDetailedEmployees()).withSelfRel());
resources.add(linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees"));

View File

@@ -27,8 +27,6 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.springframework.hateoas.Identifiable;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
@@ -37,7 +35,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
@Data
@Entity
@NoArgsConstructor
class Manager implements Identifiable<Long> {
class Manager {
@Id @GeneratedValue
private Long id;

View File

@@ -15,8 +15,8 @@
*/
package org.springframework.hateoas.examples;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.CollectionModel;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -29,9 +29,9 @@ import org.springframework.web.bind.annotation.RestController;
class ManagerController {
private final ManagerRepository repository;
private final ManagerResourceAssembler assembler;
private final ManagerRepresentationModelAssembler assembler;
ManagerController(ManagerRepository repository, ManagerResourceAssembler assembler) {
ManagerController(ManagerRepository repository, ManagerRepresentationModelAssembler assembler) {
this.repository = repository;
this.assembler = assembler;
@@ -39,28 +39,28 @@ class ManagerController {
/**
* Look up all managers, and transform them into a REST collection resource using
* {@link ManagerResourceAssembler#toResources(Iterable)}. Then return them through
* {@link ManagerRepresentationModelAssembler#toCollectionModel(Iterable)}. Then return them through
* Spring Web's {@link ResponseEntity} fluent API.
*/
@GetMapping("/managers")
ResponseEntity<Resources<Resource<Manager>>> findAll() {
ResponseEntity<CollectionModel<EntityModel<Manager>>> findAll() {
return ResponseEntity.ok(
assembler.toResources(repository.findAll()));
assembler.toCollectionModel(repository.findAll()));
}
/**
* Look up a single {@link Manager} and transform it into a REST resource using
* {@link ManagerResourceAssembler#toResource(Object)}. Then return it through
* {@link ManagerRepresentationModelAssembler#toModel(Object)}. Then return it through
* Spring Web's {@link ResponseEntity} fluent API.
*
* @param id
*/
@GetMapping("/managers/{id}")
ResponseEntity<Resource<Manager>> findOne(@PathVariable long id) {
ResponseEntity<EntityModel<Manager>> findOne(@PathVariable long id) {
return repository.findById(id)
.map(assembler::toResource)
.map(assembler::toModel)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@@ -72,8 +72,8 @@ class ManagerController {
* @return
*/
@GetMapping("/employees/{id}/manager")
ResponseEntity<Resource<Manager>> findManager(@PathVariable long id) {
ResponseEntity<EntityModel<Manager>> findManager(@PathVariable long id) {
return ResponseEntity.ok(
assembler.toResource(repository.findByEmployeesId(id)));
assembler.toModel(repository.findByEmployeesId(id)));
}
}

View File

@@ -15,30 +15,30 @@
*/
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.Resources;
import org.springframework.hateoas.SimpleIdentifiableResourceAssembler;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.SimpleIdentifiableRepresentationModelAssembler;
import org.springframework.stereotype.Component;
/**
* @author Greg Turnquist
*/
@Component
class ManagerResourceAssembler extends SimpleIdentifiableResourceAssembler<Manager> {
class ManagerRepresentationModelAssembler extends SimpleIdentifiableRepresentationModelAssembler<Manager> {
ManagerResourceAssembler() {
ManagerRepresentationModelAssembler() {
super(ManagerController.class);
}
/**
* Retain default links provided by {@link SimpleIdentifiableResourceAssembler}, but add extra ones to each {@link Manager}.
* Retain default links provided by {@link SimpleIdentifiableRepresentationModelAssembler}, but add extra ones to each {@link Manager}.
*
* @param resource
*/
@Override
public void addLinks(Resource<Manager> resource) {
public void addLinks(EntityModel<Manager> resource) {
/**
* Retain default links.
*/
@@ -57,7 +57,7 @@ class ManagerResourceAssembler extends SimpleIdentifiableResourceAssembler<Manag
* @param resources
*/
@Override
public void addLinks(Resources<Resource<Manager>> resources) {
public void addLinks(CollectionModel<EntityModel<Manager>> resources) {
super.addLinks(resources);

View File

@@ -15,9 +15,9 @@
*/
package org.springframework.hateoas.examples;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.hateoas.RepresentationModel;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -29,9 +29,9 @@ import org.springframework.web.bind.annotation.RestController;
class RootController {
@GetMapping("/")
ResponseEntity<ResourceSupport> root() {
ResponseEntity<RepresentationModel> root() {
ResourceSupport resourceSupport = new ResourceSupport();
RepresentationModel resourceSupport = new RepresentationModel();
resourceSupport.add(linkTo(methodOn(RootController.class).root()).withSelfRel());
resourceSupport.add(linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees"));

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.hateoas.examples;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -37,10 +37,10 @@ public class SupervisorController {
}
@GetMapping("/supervisors/{id}")
public ResponseEntity<Resource<Supervisor>> findOne(@PathVariable Long id) {
public ResponseEntity<EntityModel<Supervisor>> findOne(@PathVariable Long id) {
Resource<Manager> managerResource = controller.findOne(id).getBody();
Resource<Supervisor> supervisorResource = new Resource<>(
EntityModel<Manager> managerResource = controller.findOne(id).getBody();
EntityModel<Supervisor> supervisorResource = new EntityModel<>(
new Supervisor(managerResource.getContent()),
managerResource.getLinks());