mirror of
https://github.com/Example-uPagge/spring-controlleradvice.git
synced 2024-06-14 11:22:33 +03:00
RestControllerAdvice
This commit is contained in:
parent
6e104b8895
commit
0b47b9b925
@ -0,0 +1,28 @@
|
|||||||
|
package dev.struchkov.example.controlleradvice.controller;
|
||||||
|
|
||||||
|
import dev.struchkov.example.controlleradvice.domain.ErrorMessage;
|
||||||
|
import dev.struchkov.example.controlleradvice.exception.NotFoundException;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class ExceptionApiHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(NotFoundException.class)
|
||||||
|
public ResponseEntity<ErrorMessage> authException(NotFoundException exception) {
|
||||||
|
return ResponseEntity
|
||||||
|
.status(HttpStatus.NOT_FOUND)
|
||||||
|
.body(new ErrorMessage(exception.getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
||||||
|
public ResponseEntity<ErrorMessage> authException(MethodArgumentTypeMismatchException exception) {
|
||||||
|
return ResponseEntity
|
||||||
|
.status(HttpStatus.NOT_FOUND)
|
||||||
|
.body(new ErrorMessage(exception.getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,29 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user