public User findById(String id) throws EntityException { return userRepository.findById(id).orElseThrow(() -> new EntityException("No user found")); }
@GetMapping(value = {"/{userId}"}, produces = {"application/json", "application/hal+json"}) public ResponseEntity<?> findById(@PathVariable("userId") String userId) { final Mapper mapper = new Mapper(); final Optional<User> item = userRepo.findById(userId); try { final UserDto dto = mapper.from(item.orElseThrow(() -> new EntityException("No item found"))) .toInstanceOf(UserDto.class); final Resource<UserDto> resource = new Resource<>(dto); resource.add(linkTo(methodOn(getClass()).findById(userId)).withSelfRel()); return new ResponseEntity<>(resource, HttpStatus.OK); } catch (EntityException e) { return ResponseEntity.notFound().build(); } }