/** * Convenience method when chaining an existing {@link Link}. * * @param name * @param httpMethod * @param inputType * @param queryMethodParameters * @param outputType * @return */ public Link andAffordance(String name, HttpMethod httpMethod, ResolvableType inputType, List<QueryParameter> queryMethodParameters, ResolvableType outputType) { return andAffordance(new Affordance(name, this, httpMethod, inputType, queryMethodParameters, outputType)); }
/** * Convenience method when chaining an existing {@link Link}. Defaults the name of the affordance to verb + classname, e.g. * {@literal <httpMethod=HttpMethod.PUT, inputType=Employee>} produces {@literal <name=putEmployee>}. * * @param httpMethod * @param inputType * @param queryMethodParameters * @param outputType * @return */ public Link andAffordance(HttpMethod httpMethod, ResolvableType inputType, List<QueryParameter> queryMethodParameters, ResolvableType outputType) { return andAffordance(httpMethod.toString().toLowerCase() + inputType.resolve().getSimpleName(), httpMethod, inputType, queryMethodParameters, outputType); }
/** * Convenience method when chaining an existing {@link Link}. Defaults the name of the affordance to verb + classname, e.g. * {@literal <httpMethod=HttpMethod.PUT, inputType=Employee>} produces {@literal <name=putEmployee>}. * * @param httpMethod * @param inputType * @param queryMethodParameters * @param outputType * @return */ public Link andAffordance(HttpMethod httpMethod, Class<?> inputType, List<QueryParameter> queryMethodParameters, Class<?> outputType) { return andAffordance(httpMethod, ResolvableType.forClass(inputType), queryMethodParameters, ResolvableType.forClass(outputType)); }
@GetMapping("/employees") ResponseEntity<Resources<Resource<Employee>>> findAll() { List<Resource<Employee>> employeeResources = StreamSupport.stream(repository.findAll().spliterator(), false) .map(employee -> new Resource<>(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()))), linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees") )) .collect(Collectors.toList()); return ResponseEntity.ok(new Resources<>(employeeResources, linkTo(methodOn(EmployeeController.class).findAll()).withSelfRel() .andAffordance(afford(methodOn(EmployeeController.class).newEmployee(null))))); }
@GetMapping("/employees/{id}") ResponseEntity<Resource<Employee>> findOne(@PathVariable long id) { return repository.findById(id) .map(employee -> new Resource<>(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()))), linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees") )) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); }
@PostMapping("/employees") ResponseEntity<?> newEmployee(@RequestBody Employee employee) { Employee savedEmployee = repository.save(employee); return new Resource<>(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() .map(Link::getHref) .map(href -> { try { return new URI(href); } catch (URISyntaxException e) { throw new RuntimeException(e); } }) .map(uri -> ResponseEntity.noContent().location(uri).build()) .orElse(ResponseEntity.badRequest().body("Unable to create " + employee)); }
@PutMapping("/employees/{id}") ResponseEntity<?> updateEmployee(@RequestBody Employee employee, @PathVariable long id) { Employee employeeToUpdate = employee; employeeToUpdate.setId(id); Employee updatedEmployee = repository.save(employeeToUpdate); return new Resource<>(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() .map(Link::getHref) .map(href -> { try { return new URI(href); } catch (URISyntaxException e) { throw new RuntimeException(e); } }) .map(uri -> ResponseEntity.noContent().location(uri).build()) .orElse(ResponseEntity.badRequest().body("Unable to update " + employeeToUpdate)); }