Upgrade to latest Spring HATEOAS API changes.
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package org.springframework.hateoas.examples;
|
||||
|
||||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
|
||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
@@ -23,9 +23,10 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.hateoas.IanaLinkRelations;
|
||||
import org.springframework.hateoas.Link;
|
||||
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.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -49,10 +50,10 @@ class EmployeeController {
|
||||
}
|
||||
|
||||
@GetMapping("/employees")
|
||||
ResponseEntity<Resources<Resource<Employee>>> findAll() {
|
||||
ResponseEntity<CollectionModel<EntityModel<Employee>>> findAll() {
|
||||
|
||||
List<Resource<Employee>> employeeResources = StreamSupport.stream(repository.findAll().spliterator(), false)
|
||||
.map(employee -> new Resource<>(employee,
|
||||
List<EntityModel<Employee>> employeeResources = StreamSupport.stream(repository.findAll().spliterator(), false)
|
||||
.map(employee -> new EntityModel<>(employee,
|
||||
linkTo(methodOn(EmployeeController.class).findOne(employee.getId())).withSelfRel()
|
||||
.andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, employee.getId())))
|
||||
.andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(employee.getId()))),
|
||||
@@ -60,7 +61,7 @@ class EmployeeController {
|
||||
))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return ResponseEntity.ok(new Resources<>(employeeResources,
|
||||
return ResponseEntity.ok(new CollectionModel<>(employeeResources,
|
||||
linkTo(methodOn(EmployeeController.class).findAll()).withSelfRel()
|
||||
.andAffordance(afford(methodOn(EmployeeController.class).newEmployee(null)))));
|
||||
}
|
||||
@@ -70,12 +71,12 @@ class EmployeeController {
|
||||
|
||||
Employee savedEmployee = repository.save(employee);
|
||||
|
||||
return new Resource<>(savedEmployee,
|
||||
return new EntityModel<>(savedEmployee,
|
||||
linkTo(methodOn(EmployeeController.class).findOne(savedEmployee.getId())).withSelfRel()
|
||||
.andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, savedEmployee.getId())))
|
||||
.andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(savedEmployee.getId()))),
|
||||
linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees")
|
||||
).getId()
|
||||
).getLink(IanaLinkRelations.SELF)
|
||||
.map(Link::getHref)
|
||||
.map(href -> {
|
||||
try {
|
||||
@@ -89,10 +90,10 @@ class EmployeeController {
|
||||
}
|
||||
|
||||
@GetMapping("/employees/{id}")
|
||||
ResponseEntity<Resource<Employee>> findOne(@PathVariable long id) {
|
||||
ResponseEntity<EntityModel<Employee>> findOne(@PathVariable long id) {
|
||||
|
||||
return repository.findById(id)
|
||||
.map(employee -> new Resource<>(employee,
|
||||
.map(employee -> new EntityModel<>(employee,
|
||||
linkTo(methodOn(EmployeeController.class).findOne(employee.getId())).withSelfRel()
|
||||
.andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, employee.getId())))
|
||||
.andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(employee.getId()))),
|
||||
@@ -110,12 +111,12 @@ class EmployeeController {
|
||||
|
||||
Employee updatedEmployee = repository.save(employeeToUpdate);
|
||||
|
||||
return new Resource<>(updatedEmployee,
|
||||
return new EntityModel<>(updatedEmployee,
|
||||
linkTo(methodOn(EmployeeController.class).findOne(updatedEmployee.getId())).withSelfRel()
|
||||
.andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, updatedEmployee.getId())))
|
||||
.andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(updatedEmployee.getId()))),
|
||||
linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees")
|
||||
).getId()
|
||||
).getLink(IanaLinkRelations.SELF)
|
||||
.map(Link::getHref)
|
||||
.map(href -> {
|
||||
try {
|
||||
|
||||
@@ -137,4 +137,4 @@ public class EmployeeControllerTests {
|
||||
.andExpect(jsonPath("$._links.self.href", is("http://localhost/employees/1")))
|
||||
.andExpect(jsonPath("$._links.employees.href", is("http://localhost/employees")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,12 +18,12 @@ package org.springframework.hateoas.examples;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.springframework.hateoas.CollectionModel;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.MediaTypes;
|
||||
import org.springframework.hateoas.Resource;
|
||||
import org.springframework.hateoas.Resources;
|
||||
import org.springframework.hateoas.client.Traverson;
|
||||
import org.springframework.hateoas.mvc.TypeReferences.ResourcesType;
|
||||
import org.springframework.hateoas.server.core.TypeReferences.CollectionModelType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -62,9 +62,9 @@ public class HomeController {
|
||||
public String index(Model model) throws URISyntaxException {
|
||||
|
||||
Traverson client = new Traverson(new URI(REMOTE_SERVICE_ROOT_URI), MediaTypes.HAL_JSON);
|
||||
Resources<Resource<Employee>> employees = client
|
||||
CollectionModel<EntityModel<Employee>> employees = client
|
||||
.follow("employees")
|
||||
.toObject(new ResourcesType<Resource<Employee>>(){});
|
||||
.toObject(new CollectionModelType<EntityModel<Employee>>(){});
|
||||
|
||||
model.addAttribute("employee", new Employee());
|
||||
model.addAttribute("employees", employees);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -18,12 +18,12 @@ package org.springframework.hateoas.examples;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.springframework.hateoas.CollectionModel;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.MediaTypes;
|
||||
import org.springframework.hateoas.Resource;
|
||||
import org.springframework.hateoas.Resources;
|
||||
import org.springframework.hateoas.client.Traverson;
|
||||
import org.springframework.hateoas.mvc.TypeReferences.ResourcesType;
|
||||
import org.springframework.hateoas.server.core.TypeReferences.CollectionModelType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -62,9 +62,9 @@ public class HomeController {
|
||||
public String index(Model model) throws URISyntaxException {
|
||||
|
||||
Traverson client = new Traverson(new URI(REMOTE_SERVICE_ROOT_URI), MediaTypes.HAL_JSON);
|
||||
Resources<Resource<Employee>> employees = client
|
||||
CollectionModel<EntityModel<Employee>> employees = client
|
||||
.follow("employees")
|
||||
.toObject(new ResourcesType<Resource<Employee>>(){});
|
||||
.toObject(new CollectionModelType<EntityModel<Employee>>(){});
|
||||
|
||||
model.addAttribute("employee", new Employee());
|
||||
model.addAttribute("employees", employees);
|
||||
|
||||
@@ -25,15 +25,13 @@ import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
|
||||
/**
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@Entity
|
||||
class Employee implements Identifiable<Long> {
|
||||
class Employee {
|
||||
|
||||
@Id @GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@@ -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;
|
||||
@@ -34,18 +34,18 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
class EmployeeController {
|
||||
|
||||
private final EmployeeRepository repository;
|
||||
private final EmployeeResourceAssembler assembler;
|
||||
private final EmployeeRepresentationModelAssembler assembler;
|
||||
|
||||
EmployeeController(EmployeeRepository repository, EmployeeResourceAssembler assembler) {
|
||||
EmployeeController(EmployeeRepository repository, EmployeeRepresentationModelAssembler assembler) {
|
||||
|
||||
this.repository = repository;
|
||||
this.assembler = assembler;
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public ResourceSupport root() {
|
||||
public RepresentationModel root() {
|
||||
|
||||
ResourceSupport rootResource = new ResourceSupport();
|
||||
RepresentationModel rootResource = new RepresentationModel();
|
||||
|
||||
rootResource.add(
|
||||
linkTo(methodOn(EmployeeController.class).root()).withSelfRel(),
|
||||
@@ -55,12 +55,12 @@ 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);
|
||||
|
||||
@@ -68,13 +68,13 @@ class EmployeeController {
|
||||
.created(savedEmployee.getId()
|
||||
.map(id -> linkTo(methodOn(EmployeeController.class).findOne(id)).toUri())
|
||||
.orElseThrow(() -> new RuntimeException("Failed to create for some reason")))
|
||||
.body(assembler.toResource(savedEmployee));
|
||||
.body(assembler.toModel(savedEmployee));
|
||||
}
|
||||
|
||||
@GetMapping("/employees/{id}")
|
||||
public Resource<Employee> findOne(@PathVariable Long id) {
|
||||
public EntityModel<Employee> findOne(@PathVariable Long id) {
|
||||
return repository.findById(id)
|
||||
.map(assembler::toResource)
|
||||
.map(assembler::toModel)
|
||||
.orElseThrow(() -> new RuntimeException("No employee '" + id + "' found"));
|
||||
}
|
||||
|
||||
|
||||
@@ -15,16 +15,16 @@
|
||||
*/
|
||||
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 EmployeeRepresentationModelAssembler extends SimpleIdentifiableRepresentationModelAssembler<Employee> {
|
||||
|
||||
EmployeeResourceAssembler() {
|
||||
EmployeeRepresentationModelAssembler() {
|
||||
super(EmployeeController.class);
|
||||
}
|
||||
}
|
||||
@@ -26,8 +26,6 @@ import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
/**
|
||||
@@ -51,7 +49,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@AllArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
class Employee implements Identifiable<Long> {
|
||||
class Employee {
|
||||
|
||||
@Id @GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@@ -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;
|
||||
@@ -25,7 +25,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
/**
|
||||
* Spring Web {@link RestController} used to generate a REST API.
|
||||
*
|
||||
* Works by injecting an {@link EmployeeRepository} and an {@link EmployeeResourceAssembler} in the constructor, both
|
||||
* Works by injecting an {@link EmployeeRepository} and an {@link EmployeeRepresentationModelAssembler} in the constructor, both
|
||||
* of which are used to retrieve data from the database, and assemble a REST resource.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
@@ -34,9 +34,9 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
class EmployeeController {
|
||||
|
||||
private final EmployeeRepository repository;
|
||||
private final EmployeeResourceAssembler assembler;
|
||||
private final EmployeeRepresentationModelAssembler assembler;
|
||||
|
||||
EmployeeController(EmployeeRepository repository, EmployeeResourceAssembler assembler) {
|
||||
EmployeeController(EmployeeRepository repository, EmployeeRepresentationModelAssembler assembler) {
|
||||
|
||||
this.repository = repository;
|
||||
this.assembler = assembler;
|
||||
@@ -44,29 +44,29 @@ 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(
|
||||
this.assembler.toResources(this.repository.findAll()));
|
||||
this.assembler.toCollectionModel(this.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 this.repository.findById(id)
|
||||
.map(this.assembler::toResource)
|
||||
.map(this.assembler::toModel)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@@ -15,21 +15,21 @@
|
||||
*/
|
||||
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 EmployeeRepresentationModelAssembler extends SimpleIdentifiableRepresentationModelAssembler<Employee> {
|
||||
|
||||
/**
|
||||
* Link the {@link Employee} domain type to the {@link EmployeeController} using this
|
||||
* {@link SimpleIdentifiableResourceAssembler} in order to generate both {@link org.springframework.hateoas.Resource}
|
||||
* and {@link org.springframework.hateoas.Resources}.
|
||||
* {@link SimpleIdentifiableRepresentationModelAssembler} in order to generate both {@link org.springframework.hateoas.Resource}
|
||||
* and {@link org.springframework.hateoas.CollectionModel}.
|
||||
*/
|
||||
EmployeeResourceAssembler() {
|
||||
EmployeeRepresentationModelAssembler() {
|
||||
super(EmployeeController.class);
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ package org.springframework.hateoas.examples;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.hateoas.core.EvoInflectorRelProvider;
|
||||
import org.springframework.hateoas.server.core.EvoInflectorRelProvider;
|
||||
|
||||
/**
|
||||
* @author Greg Turnquist
|
||||
|
||||
@@ -42,7 +42,7 @@ import org.springframework.test.web.servlet.MockMvc;
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(EmployeeController.class)
|
||||
@Import({EmployeeResourceAssembler.class})
|
||||
@Import({EmployeeRepresentationModelAssembler.class})
|
||||
public class EmployeeControllerTests {
|
||||
|
||||
@Autowired
|
||||
|
||||
@@ -15,22 +15,28 @@
|
||||
*/
|
||||
package org.springframework.hateoas;
|
||||
|
||||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
|
||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.hateoas.core.EvoInflectorRelProvider;
|
||||
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
|
||||
import org.springframework.hateoas.server.LinkBuilder;
|
||||
import org.springframework.hateoas.server.RelProvider;
|
||||
import org.springframework.hateoas.server.SimpleRepresentationModelAssembler;
|
||||
import org.springframework.hateoas.server.core.EvoInflectorRelProvider;
|
||||
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* A {@link SimpleResourceAssembler} that mixes together a Spring web controller and a {@link RelProvider} to build links
|
||||
* A {@link SimpleRepresentationModelAssembler} that mixes together a Spring web controller and a {@link RelProvider} to build links
|
||||
* upon a certain strategy.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
public class SimpleIdentifiableResourceAssembler<T extends Identifiable<?>> implements SimpleResourceAssembler<T> {
|
||||
public class SimpleIdentifiableRepresentationModelAssembler<T> implements SimpleRepresentationModelAssembler<T> {
|
||||
|
||||
/**
|
||||
* The Spring MVC class for the {@link Identifiable} from which links will be built.
|
||||
@@ -61,13 +67,13 @@ public class SimpleIdentifiableResourceAssembler<T extends Identifiable<?>> impl
|
||||
* @param controllerClass - Spring MVC controller to base links off of
|
||||
* @param relProvider
|
||||
*/
|
||||
public SimpleIdentifiableResourceAssembler(Class<?> controllerClass, RelProvider relProvider) {
|
||||
public SimpleIdentifiableRepresentationModelAssembler(Class<?> controllerClass, RelProvider relProvider) {
|
||||
|
||||
this.controllerClass = controllerClass;
|
||||
this.relProvider = relProvider;
|
||||
|
||||
// Find the "T" type contained in "T extends Identifiable<?>", e.g. SimpleIdentifiableResourceAssembler<User> -> User
|
||||
this.resourceType = GenericTypeResolver.resolveTypeArgument(this.getClass(), SimpleIdentifiableResourceAssembler.class);
|
||||
// Find the "T" type contained in "T extends Identifiable<?>", e.g. SimpleIdentifiableRepresentationModelAssembler<User> -> User
|
||||
this.resourceType = GenericTypeResolver.resolveTypeArgument(this.getClass(), SimpleIdentifiableRepresentationModelAssembler.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,7 +81,7 @@ public class SimpleIdentifiableResourceAssembler<T extends Identifiable<?>> impl
|
||||
*
|
||||
* @param controllerClass
|
||||
*/
|
||||
public SimpleIdentifiableResourceAssembler(Class<?> controllerClass) {
|
||||
public SimpleIdentifiableRepresentationModelAssembler(Class<?> controllerClass) {
|
||||
this(controllerClass, new EvoInflectorRelProvider());
|
||||
}
|
||||
|
||||
@@ -85,20 +91,26 @@ public class SimpleIdentifiableResourceAssembler<T extends Identifiable<?>> impl
|
||||
*
|
||||
* @param resource
|
||||
*/
|
||||
@Override
|
||||
public void addLinks(Resource<T> resource) {
|
||||
public void addLinks(EntityModel<T> resource) {
|
||||
|
||||
resource.add(getCollectionLinkBuilder().slash(resource.getContent()).withSelfRel());
|
||||
resource.add(getCollectionLinkBuilder().slash(getId(resource)).withSelfRel());
|
||||
resource.add(getCollectionLinkBuilder().withRel(this.relProvider.getCollectionResourceRelFor(this.resourceType)));
|
||||
}
|
||||
|
||||
private Object getId(EntityModel<T> resource) {
|
||||
|
||||
Field id = ReflectionUtils.findField(this.resourceType, "id");
|
||||
ReflectionUtils.makeAccessible(id);
|
||||
|
||||
return ReflectionUtils.getField(id, resource.getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a self link to the aggregate root.
|
||||
*
|
||||
* @param resources
|
||||
*/
|
||||
@Override
|
||||
public void addLinks(Resources<Resource<T>> resources) {
|
||||
public void addLinks(CollectionModel<EntityModel<T>> resources) {
|
||||
resources.add(getCollectionLinkBuilder().withSelfRel());
|
||||
}
|
||||
|
||||
@@ -110,14 +122,14 @@ public class SimpleIdentifiableResourceAssembler<T extends Identifiable<?>> impl
|
||||
* objects will be serving resources at {@code /employees} and {@code /employees/1}.
|
||||
*
|
||||
* If this is not the case, simply override this method in your concrete instance, or resort to
|
||||
* overriding {@link #addLinks(Resource)} and {@link #addLinks(Resources)} where you have full control over exactly
|
||||
* overriding {@link #addLinks(EntityModel)} and {@link #addLinks(CollectionModel)} where you have full control over exactly
|
||||
* what links are put in the individual and collection resources.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected LinkBuilder getCollectionLinkBuilder() {
|
||||
|
||||
ControllerLinkBuilder linkBuilder = linkTo(this.controllerClass);
|
||||
WebMvcLinkBuilder linkBuilder = linkTo(this.controllerClass);
|
||||
|
||||
for (String pathComponent : (getPrefix() + this.relProvider.getCollectionResourceRelFor(this.resourceType)).split("/")) {
|
||||
if (!pathComponent.isEmpty()) {
|
||||
@@ -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;
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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"));
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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"));
|
||||
|
||||
@@ -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());
|
||||
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -39,7 +39,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.2.RELEASE</version>
|
||||
<version>2.2.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package org.springframework.hateoas.examples;
|
||||
|
||||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
|
||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
@@ -23,9 +23,10 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.hateoas.IanaLinkRelations;
|
||||
import org.springframework.hateoas.Link;
|
||||
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;
|
||||
@@ -53,16 +54,16 @@ class EmployeeController {
|
||||
* Then return them through Spring Web's {@link ResponseEntity} fluent API.
|
||||
*/
|
||||
@GetMapping("/employees")
|
||||
ResponseEntity<Resources<Resource<Employee>>> findAll() {
|
||||
ResponseEntity<CollectionModel<EntityModel<Employee>>> findAll() {
|
||||
|
||||
List<Resource<Employee>> employees = StreamSupport.stream(repository.findAll().spliterator(), false)
|
||||
.map(employee -> new Resource<>(employee,
|
||||
List<EntityModel<Employee>> employees = StreamSupport.stream(repository.findAll().spliterator(), false)
|
||||
.map(employee -> new EntityModel<>(employee,
|
||||
linkTo(methodOn(EmployeeController.class).findOne(employee.getId())).withSelfRel(),
|
||||
linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees")))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return ResponseEntity.ok(
|
||||
new Resources<>(employees,
|
||||
new CollectionModel<>(employees,
|
||||
linkTo(methodOn(EmployeeController.class).findAll()).withSelfRel()));
|
||||
}
|
||||
|
||||
@@ -72,11 +73,11 @@ class EmployeeController {
|
||||
try {
|
||||
Employee savedEmployee = repository.save(employee);
|
||||
|
||||
Resource<Employee> employeeResource = new Resource<>(savedEmployee,
|
||||
EntityModel<Employee> employeeResource = new EntityModel<>(savedEmployee,
|
||||
linkTo(methodOn(EmployeeController.class).findOne(savedEmployee.getId())).withSelfRel());
|
||||
|
||||
return ResponseEntity
|
||||
.created(new URI(employeeResource.getRequiredLink(Link.REL_SELF).getHref()))
|
||||
.created(new URI(employeeResource.getRequiredLink(IanaLinkRelations.SELF).getHref()))
|
||||
.body(employeeResource);
|
||||
} catch (URISyntaxException e) {
|
||||
return ResponseEntity.badRequest().body("Unable to create " + employee);
|
||||
@@ -90,10 +91,10 @@ class EmployeeController {
|
||||
* @param id
|
||||
*/
|
||||
@GetMapping("/employees/{id}")
|
||||
ResponseEntity<Resource<Employee>> findOne(@PathVariable long id) {
|
||||
ResponseEntity<EntityModel<Employee>> findOne(@PathVariable long id) {
|
||||
|
||||
return repository.findById(id)
|
||||
.map(employee -> new Resource<>(employee,
|
||||
.map(employee -> new EntityModel<>(employee,
|
||||
linkTo(methodOn(EmployeeController.class).findOne(employee.getId())).withSelfRel(),
|
||||
linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees")))
|
||||
.map(ResponseEntity::ok)
|
||||
|
||||
@@ -19,7 +19,7 @@ package org.springframework.hateoas.examples;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.hateoas.core.EvoInflectorRelProvider;
|
||||
import org.springframework.hateoas.server.core.EvoInflectorRelProvider;
|
||||
|
||||
/**
|
||||
* @author Greg Turnquist
|
||||
|
||||
Reference in New Issue
Block a user