Spring Data Rest exception handling - Return generic error response

前端 未结 2 1393
一向
一向 2021-02-06 09:55

I want to know how can I handle internal server error type exceptions in Spring Data Rest such as JPA exceptions etc. due to a malformed request or a database crash. I did some

2条回答
  •  星月不相逢
    2021-02-06 10:29

    You can do it like this:

    @ControllerAdvice(basePackageClasses = RepositoryRestExceptionHandler.class)
    public class GenericExceptionHandler {
    
        @ExceptionHandler
        ResponseEntity handle(Exception e) {
            return new ResponseEntity("Some message", new HttpHeaders(), HttpStatus.BAD_REQUEST);
        }
    }
    

提交回复
热议问题