Hi,

I created a RestExceptionHandler to manage all exceptions thrown from RestControllers. I am having a problem that RestExceptionHandler is not intercepting the exception and returning the custom message format.

The RestExceptionhandler implementation:

@Order(Ordered.HIGHEST_PRECEDENCE) 
@ControllerAdvice(annotations = RestController.class)
public class RestExceptionHandler

    private static final Logger LOGGER = LoggerFactory.getLogger(RestExceptionHandler.class);

    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<ErrorDetail> handleResourceNotFoundException(ResourceNotFoundException exception,
            HttpServletRequest request) {
        ErrorDetail errorDetail = new ErrorDetail();
        errorDetail.setTimeStamp(Instant.now().getEpochSecond());
        errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
        errorDetail.setTitle("Resource Not Found");
        errorDetail.setDetail(exception.getMessage());
        errorDetail.setDeveloperMessage(exception.getClass().getName());
        return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
    }
}

The RestController implementation as below:

@RestController
@RequestMapping("/departments")
public class DepartmentRestController {

    @Autowired
    private DepartmentService departmentService;

    protected Department verifyDeparmentExistence(int id) {
        Department department = departmentService.findById(id);
        if (department != null) {
            throw new ResourceNotFoundException("Department with id: " + id + " not found.");
        }
        return department;
    }

    @GetMapping
    public ResponseEntity<List<Department>> findAll() {
        List<Department> departmentList = departmentService.findAll();
        return new ResponseEntity<>(departmentList, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Department> findById(@PathVariable int id) {
        Department department = verifyDeparmentExistence(id);
        return new ResponseEntity<>(department, HttpStatus.OK);
    }
}

The ResourceNotFoundException is like below:

@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public ResourceNotFoundException() {}

    public ResourceNotFoundException(String message) {
        super(message);
    }

    public ResourceNotFoundException(String message, Throwable cause) {
        super(message,cause);
    }

}

The ErrorDetail (the custom message format that should be returned) :

@Data
public class ErrorDetail {

    private String title;
    private int status;
    private String detail;
    private long timeStamp;
    private String path;
    private String developerMessage;
    private Map<String, List<ValidationError>> errors = new HashMap<>();
}

The full project you can find it the link below : https://github.com/rshtishi/payroll/tree/master/department

Comment From: rshtishi

Hello,

I fixed the issue. Another exception was thrown before ResourceNotFoundException. That was caught by GlobalExceptionHanlder since I didn't specify a handler for that type of exception in my custom RestExceptionhandler. I fixed that and everything is working as it should. You can close the issue.