@GetMapping(value = "/employees/{id}", produces = MediaTypes.HAL_JSON_VALUE) public Resource<Employee> findOne(@PathVariable Long id) { return repository.findById(id) .map(assembler::toResource) .orElseThrow(() -> new RuntimeException("No employee '" + id + "' found")); }
@GetMapping(value = "/employees/{id}/detailed", produces = MediaTypes.HAL_JSON_VALUE) public ResponseEntity<Resource<EmployeeWithManager>> findDetailedEmployee(@PathVariable Long id) { return repository.findById(id) .map(EmployeeWithManager::new) .map(employeeWithManagerResourceAssembler::toResource) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); } }
@GetMapping(value = "/employees/{id}", produces = MediaTypes.HAL_JSON_VALUE) public ResponseEntity<Resource<Employee>> findOne(@PathVariable Long id) { return repository.findById(id) .map(assembler::toResource) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); }
/** * Look up a single {@link Employee} and transform it into a REST resource using * {@link EmployeeResourceAssembler#toResource(Object)}. Then return it through * Spring Web's {@link ResponseEntity} fluent API. * * See {@link #findAll()} to explain {@link GetMapping}'s "produces" argument. * * @param id */ @GetMapping(value = "/employees/{id}", produces = MediaTypes.HAL_JSON_VALUE) public ResponseEntity<Resource<Employee>> findOne(@PathVariable long id) { return repository.findById(id) .map(assembler::toResource) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); }
/** * Look up a single {@link Employee} and transform it into a REST resource using * {@link EmployeeResourceAssembler#toResource(Object)}. Then return it through * Spring Web's {@link ResponseEntity} fluent API. * * See {@link #findAll()} to explain {@link GetMapping}'s "produces" argument. * * @param id */ @GetMapping(value = "/employees/{id}", produces = MediaTypes.HAL_JSON_VALUE) public ResponseEntity<Resource<Employee>> findOne(@PathVariable long id) { return repository.findById(id) .map(assembler::toResource) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); }
/** * Look up a single {@link Employee} and transform it into a REST resource. Then return it through * Spring Web's {@link ResponseEntity} fluent API. * * See {@link #findAll()} to explain {@link GetMapping}'s "produces" argument. * * @param id */ @GetMapping(value = "/employees/{id}", produces = MediaTypes.HAL_JSON_VALUE) ResponseEntity<Resource<Employee>> findOne(@PathVariable long id) { return repository.findById(id) .map(employee -> new Resource<>(employee, linkTo(methodOn(EmployeeController.class).findOne(employee.getId())).withSelfRel(), linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees"))) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); }
@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()); }