diff --git a/src/main/java/dev/struchkov/example/controlleradvice/controller/PersonController.java b/src/main/java/dev/struchkov/example/controlleradvice/controller/PersonController.java index 969358b..4577ede 100644 --- a/src/main/java/dev/struchkov/example/controlleradvice/controller/PersonController.java +++ b/src/main/java/dev/struchkov/example/controlleradvice/controller/PersonController.java @@ -1,21 +1,15 @@ package dev.struchkov.example.controlleradvice.controller; -import dev.struchkov.example.controlleradvice.domain.ErrorMessage; import dev.struchkov.example.controlleradvice.domain.Person; -import dev.struchkov.example.controlleradvice.exception.NotFoundException; import dev.struchkov.example.controlleradvice.service.PersonService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import java.util.UUID; @@ -37,39 +31,4 @@ public class PersonController { return ResponseEntity.ok(personService.getById(id).orElseThrow()); } - // Ниже методы предназначенные для обработки исключений. - - @ExceptionHandler(NotFoundException.class) - public ResponseEntity handleException(NotFoundException exception) { - log.error(exception.getMessage(), exception); - return ResponseEntity - .status(HttpStatus.NOT_FOUND) - .body(new ErrorMessage(exception.getMessage())); - } - -// Вариант handleException без использованияResponseEntity. -// @ResponseStatus(HttpStatus.NOT_FOUND) -// @ExceptionHandler(NotFoundException.class) -// public ErrorMessage handleException(NotFoundException exception) { -// return new ErrorMessage(exception.getMessage()); -// } - - /** - * Не будет вызываться для getByLogin, пока не будет закомментирован обработчик @ExceptionHandler(NotFoundException.class). - * Но будет вызываться для getById - */ - @ExceptionHandler(RuntimeException.class) - public ResponseEntity handleException(RuntimeException exception) { - return ResponseEntity - .status(HttpStatus.NOT_FOUND) - .body(new ErrorMessage(exception.getMessage())); - } - - // Обработка ошибки преобразования - @ResponseStatus(HttpStatus.NOT_FOUND) - @ExceptionHandler(MethodArgumentTypeMismatchException.class) - public ErrorMessage handleException(MethodArgumentTypeMismatchException exception) { - return new ErrorMessage(exception.getMessage()); - } - } diff --git a/src/main/java/dev/struchkov/example/controlleradvice/service/CustomExceptionResolver.java b/src/main/java/dev/struchkov/example/controlleradvice/service/CustomExceptionResolver.java new file mode 100644 index 0000000..3335524 --- /dev/null +++ b/src/main/java/dev/struchkov/example/controlleradvice/service/CustomExceptionResolver.java @@ -0,0 +1,29 @@ +package dev.struchkov.example.controlleradvice.service; + +import dev.struchkov.example.controlleradvice.exception.NotFoundException; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver; +import org.springframework.web.servlet.view.json.MappingJackson2JsonView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Component +public class CustomExceptionResolver extends AbstractHandlerExceptionResolver { + + @Override + protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) { + final ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView()); + if (e instanceof NotFoundException) { + modelAndView.setStatus(HttpStatus.NOT_FOUND); + modelAndView.addObject("message", "Пользователь не найден"); + return modelAndView; + } + modelAndView.setStatus(HttpStatus.INTERNAL_SERVER_ERROR); + modelAndView.addObject("message", "При выполнении запроса произошла ошибка"); + return modelAndView; + } + +}