Predicate<? super User> predicate = user -> user.getId().equals(id);
return users.stream().filter(predicate).findFirst().orElse(null);
@GetMapping("/users/{id}")
public User retrieveUsers(@PathVariable int id){
User user = service.findOne(id);
if(user==null)
throw new UserNotFoundException("id:" + id);
return service.findOne(id);
}
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException (String message) {
super(message);
}
}
보통 존재하지 않는건 404페이지 처리를 해주기 때문에(지금은 500) 404로 바꿔준다
@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException (String message) {
super(message);
}
}
변경 완료!
*devtools를 주석처리해주면 로그가 짧게 뜸