Rafael
Rafael

Reputation: 2676

global exception handling for rest exposed spring-data

Using spring-data-rest to expose repositories i want to overwrite the default exception handling.

reading documentation it looks to me that the best wat would be using a @ControllerAdvice annotated class

@ControllerAdvice
class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler {

    Logger log = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class);

    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> badRequest(HttpServletRequest req, Exception exception) {
        log.info("++++ GLOBAL EXCEPTION HANDLING ++++");
        return null;
    }
}

There are several point i am not sure about:

By the way this does not seems to work even when i've tried different configurations. Is there a way to customize error handling in spring-data-rest?

Upvotes: 2

Views: 1965

Answers (1)

Raphael Amoedo
Raphael Amoedo

Reputation: 4475

It's missing RepositoryRestExceptionHandler. Would be something like this:

Like this:

@ControllerAdvice(basePackageClasses = RepositoryRestExceptionHandler.class)
public class GlobalControllerExceptionHandler {

    Logger log = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class);

    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> badRequest(HttpServletRequest req, Exception exception) {
        log.info("++++ GLOBAL EXCEPTION HANDLING ++++");
        return null;
    }
}

Upvotes: 1

Related Questions