Polishing.
This commit is contained in:
@@ -19,8 +19,8 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* An updated domain object on the client side. It doesn't need all the backward compatible bits that the new
|
||||
* server needs (unless this becomes a service of its own).
|
||||
* An updated domain object on the client side. It doesn't need all the backward compatible bits that the new server
|
||||
* needs (unless this becomes a service of its own).
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
|
||||
@@ -48,11 +48,9 @@ public class HomeController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a listing of ALL {@link Employee}s by querying the remote services' root URI, and then
|
||||
* "hopping" to the {@literal employees} rel.
|
||||
*
|
||||
* NOTE: Also create a form-backed {@link Employee} object to allow creating a new entry with
|
||||
* the Thymeleaf template.
|
||||
* Get a listing of ALL {@link Employee}s by querying the remote services' root URI, and then "hopping" to the
|
||||
* {@literal employees} rel. NOTE: Also create a form-backed {@link Employee} object to allow creating a new entry
|
||||
* with the Thymeleaf template.
|
||||
*
|
||||
* @param model
|
||||
* @return
|
||||
@@ -62,9 +60,10 @@ public class HomeController {
|
||||
public String index(Model model) throws URISyntaxException {
|
||||
|
||||
Traverson client = new Traverson(new URI(REMOTE_SERVICE_ROOT_URI), MediaTypes.HAL_JSON);
|
||||
CollectionModel<EntityModel<Employee>> employees = client
|
||||
.follow("employees")
|
||||
.toObject(new CollectionModelType<EntityModel<Employee>>(){});
|
||||
|
||||
CollectionModel<EntityModel<Employee>> employees = client //
|
||||
.follow("employees") //
|
||||
.toObject(new CollectionModelType<EntityModel<Employee>>() {});
|
||||
|
||||
model.addAttribute("employee", new Employee());
|
||||
model.addAttribute("employees", employees);
|
||||
@@ -73,11 +72,9 @@ public class HomeController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Instead of putting the creation link from the remote service in the template (a security concern),
|
||||
* have a local route for {@literal POST} requests. Gather up the information, and form a remote call,
|
||||
* using {@link Traverson} to fetch the {@literal employees} {@link Link}.
|
||||
*
|
||||
* Once a new employee is created, redirect back to the root URL.
|
||||
* Instead of putting the creation link from the remote service in the template (a security concern), have a local
|
||||
* route for {@literal POST} requests. Gather up the information, and form a remote call, using {@link Traverson} to
|
||||
* fetch the {@literal employees} {@link Link}. Once a new employee is created, redirect back to the root URL.
|
||||
*
|
||||
* @param employee
|
||||
* @return
|
||||
@@ -87,9 +84,7 @@ public class HomeController {
|
||||
public String newEmployee(@ModelAttribute Employee employee) throws URISyntaxException {
|
||||
|
||||
Traverson client = new Traverson(new URI(REMOTE_SERVICE_ROOT_URI), MediaTypes.HAL_JSON);
|
||||
Link employeesLink = client
|
||||
.follow("employees")
|
||||
.asLink();
|
||||
Link employeesLink = client.follow("employees").asLink();
|
||||
|
||||
this.rest.postForEntity(employeesLink.expand().getHref(), employee, Employee.class);
|
||||
|
||||
|
||||
@@ -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"));
|
||||
|
||||
@@ -48,11 +48,9 @@ public class HomeController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a listing of ALL {@link Employee}s by querying the remote services' root URI, and then
|
||||
* "hopping" to the {@literal employees} rel.
|
||||
*
|
||||
* NOTE: Also create a form-backed {@link Employee} object to allow creating a new entry with
|
||||
* the Thymeleaf template.
|
||||
* Get a listing of ALL {@link Employee}s by querying the remote services' root URI, and then "hopping" to the
|
||||
* {@literal employees} rel. NOTE: Also create a form-backed {@link Employee} object to allow creating a new entry
|
||||
* with the Thymeleaf template.
|
||||
*
|
||||
* @param model
|
||||
* @return
|
||||
@@ -62,9 +60,9 @@ public class HomeController {
|
||||
public String index(Model model) throws URISyntaxException {
|
||||
|
||||
Traverson client = new Traverson(new URI(REMOTE_SERVICE_ROOT_URI), MediaTypes.HAL_JSON);
|
||||
CollectionModel<EntityModel<Employee>> employees = client
|
||||
.follow("employees")
|
||||
.toObject(new CollectionModelType<EntityModel<Employee>>(){});
|
||||
CollectionModel<EntityModel<Employee>> employees = client //
|
||||
.follow("employees") //
|
||||
.toObject(new CollectionModelType<EntityModel<Employee>>() {});
|
||||
|
||||
model.addAttribute("employee", new Employee());
|
||||
model.addAttribute("employees", employees);
|
||||
@@ -73,11 +71,9 @@ public class HomeController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Instead of putting the creation link from the remote service in the template (a security concern),
|
||||
* have a local route for {@literal POST} requests. Gather up the information, and form a remote call,
|
||||
* using {@link Traverson} to fetch the {@literal employees} {@link Link}.
|
||||
*
|
||||
* Once a new employee is created, redirect back to the root URL.
|
||||
* Instead of putting the creation link from the remote service in the template (a security concern), have a local
|
||||
* route for {@literal POST} requests. Gather up the information, and form a remote call, using {@link Traverson} to
|
||||
* fetch the {@literal employees} {@link Link}. Once a new employee is created, redirect back to the root URL.
|
||||
*
|
||||
* @param employee
|
||||
* @return
|
||||
@@ -87,9 +83,9 @@ public class HomeController {
|
||||
public String newEmployee(@ModelAttribute Employee employee) throws URISyntaxException {
|
||||
|
||||
Traverson client = new Traverson(new URI(REMOTE_SERVICE_ROOT_URI), MediaTypes.HAL_JSON);
|
||||
Link employeesLink = client
|
||||
.follow("employees")
|
||||
.asLink();
|
||||
Link employeesLink = client //
|
||||
.follow("employees") //
|
||||
.asLink();
|
||||
|
||||
this.rest.postForEntity(employeesLink.expand().getHref(), employee, Employee.class);
|
||||
|
||||
|
||||
@@ -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