spring 我应该在Sping Boot 中为空结果返回EntityNotFoundException吗?[已关闭]

xzlaal3s  于 2022-11-28  发布在  Spring
关注(0)|答案(1)|浏览(143)

已关闭。此问题为opinion-based。当前不接受答案。
**想要改进此问题吗?**请更新问题,以便editing this post可以使用事实与引用来回答.

2天前关闭。
Improve this question
在Sping Boot 应用程序中,我使用@RestControllerAdvice创建了一个全局异常处理程序类,如下所示:

  1. @Slf4j(topic = "GLOBAL_EXCEPTION_HANDLER")
  2. @RestControllerAdvice
  3. public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {-
  4. @Override
  5. @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
  6. protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
  7. HttpHeaders headers,
  8. HttpStatus status,
  9. WebRequest request) {
  10. ErrorResponse errorResponse = new ErrorResponse(HttpStatus.UNPROCESSABLE_ENTITY.value(), VALIDATION_ERROR);
  11. for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
  12. errorResponse.addValidationError(fieldError.getField(), fieldError.getDefaultMessage());
  13. }
  14. return ResponseEntity.unprocessableEntity().body(errorResponse);
  15. }
  16. @ExceptionHandler(EntityNotFoundException.class)
  17. @ResponseStatus(HttpStatus.NOT_FOUND)
  18. public ResponseEntity<Object> handleEntityNotFoundException(EntityNotFoundException ex,
  19. WebRequest request) {
  20. log.error(ENTITY_NOT_FOUND, ex);
  21. return buildErrorResponse(ex, HttpStatus.NOT_FOUND, request);
  22. }
  23. @ExceptionHandler(Exception.class)
  24. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  25. public ResponseEntity<Object> handleAllUncaughtException(Exception ex, WebRequest request) {
  26. log.error(UNKNOWN_ERROR, ex);
  27. return buildErrorResponse(ex, UNKNOWN_ERROR, HttpStatus.INTERNAL_SERVER_ERROR, request);
  28. }
  29. private ResponseEntity<Object> buildErrorResponse(Exception ex,
  30. HttpStatus httpStatus,
  31. WebRequest request) {
  32. return buildErrorResponse(ex, ex.getMessage(), httpStatus, request);
  33. }
  34. private ResponseEntity<Object> buildErrorResponse(Exception ex,
  35. String message,
  36. HttpStatus httpStatus,
  37. WebRequest request) {
  38. ErrorResponse errorResponse = new ErrorResponse(httpStatus.value(), message);
  39. if (printStackTrace && isTraceOn(request)) {
  40. errorResponse.setStackTrace(ExceptionUtils.getStackTrace(ex));
  41. }
  42. return ResponseEntity.status(httpStatus).body(errorResponse);
  43. }
  44. @Override
  45. public ResponseEntity<Object> handleExceptionInternal(
  46. Exception ex,
  47. Object body,
  48. HttpHeaders headers,
  49. HttpStatus status,
  50. WebRequest request) {
  51. return buildErrorResponse(ex, status, request);
  52. }
  53. }

我的服务方法如下所示:

  1. private List<Employee> findByName(String name) {
  2. List<Employee> employees = employeeRepo.findByName(name);
  3. // At this stage, should I check if the result is empty list and then throw exception?
  4. if (employees.isEmpty()) {
  5. throw new EntityNotFoundException("Not found any employee");
  6. }
  7. return employees;
  8. }

在返回员工列表之前,是否应该检查结果是否为空列表,然后抛出异常?

bxgwgixi

bxgwgixi1#

这取决于具体情况和您希望实现的目标。这是一种向请求客户端显示没有员工使用所请求的名称的方式。您应该将“runways.isEmpty()”替换为“employees.isEmpty()”,否则可能会在构建过程中出错。
由于您已经在使用自定义的ExceptionHandler,因此可以轻松地抛出Exception,然后使用您自己的自定义异常消息中断异常消息。
另一个不抛出异常的选项是,您可以只返回空列表,如果您有一个FE应用程序,您可以检查列表是否为空,并相应地向客户端显示相应的消息。

相关问题