Im trying to configure @RestControllerAdvice
for my Spring Boot application, but here is some trouble:
im throwing my custom EmployeeNotFoundException
like here
public void delete(long employeeId) throws Exception { if(employeeDao.ifExist(employeeId)) employeeDao.delete(employeeId); else throw new EmployeeNotFoundException(employeeId); employees = employeeDao.read(); logToConsole();}
so some other standard exceptions cannot be thrown, but i got exception warnings from swagger: swagger-errors (i have no authorization - 401 Unauthorized cannot be, i have no acces management - 403 Forbidden)
@RestControllerAdvice
:
@ExceptionHandler(value = { EmployeeNotFoundException.class }) public ResponseEntity<Object> handleEmployeeNotFoundException(EmployeeNotFoundException e) { e.printStackTrace(); System.out.println("Make sure u trying to manage existing employee"); return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); } @ResponseStatus(value = HttpStatus.FORBIDDEN) @ExceptionHandler(Exception.class) public ResponseEntity<Object> exception(Exception ex) { return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); }
So here is the question: How can i remove standard error responses? How can i use my custom exception instead of standard 404 (i want to override 404)?