在spring boot中是否可以用一个@ExceptionHandler来处理多个异常?

问题描述 投票:0回答:2

我有这段代码,我应该在其中返回自定义的错误消息和错误代码。它工作正常,但问题是它非常多余。我想知道是否可以用一个 @ExceptionHandler 来处理所有这些自定义异常。

@ControllerAdvice
public class HandleExceptions {

    @ExceptionHandler(value = CustomException1.class)
    public ResponseEntity<Object> handleCustomException1(CustomException1 e) {
        HttpStatus status = HttpStatus.NOT_FOUND;
        DataResponse data = new DataResponse("custom status code for exception 1", e.getMessage());
        ExceptionResponse exceptionResponse = new ExceptionResponse(data);
        return new ResponseEntity<>(ExceptionResponse, status);
    }
    
    @ExceptionHandler(value = CustomException2.class)
    public ResponseEntity<Object> handleCustomException2(CustomException2 e) {
        HttpStatus status = HttpStatus.NOT_FOUND;
        DataResponse data = new DataResponse("custom status code for exception 2", e.getMessage());
        ExceptionResponse exceptionResponse = new ExceptionResponse(data);
        return new ResponseEntity<>(ExceptionResponse, status);
    }
    
    @ExceptionHandler(value = CustomException3.class)
    public ResponseEntity<Object> handleCustomException3(CustomException3 e) {
        HttpStatus status = HttpStatus.BAD_REQUEST;
        DataResponse data = new DataResponse("custom status code for exception 3", e.getMessage());
        ExceptionResponse exceptionResponse = new ExceptionResponse(data);
        return new ResponseEntity<>(ExceptionResponse, status);
    }

public class ExceptionResponse {
private final DataResponse data;
}
private String statusCode;
private String message;
}

自定义错误类都是这样的:

public class CustomException extends RuntimeException{public CustomException(String message){         super(message);     
}

响应的一个例子是:

Data: {
 status: 1111,
 message: user not found } 
java spring-boot refactoring redundancy exceptionhandler
2个回答
0
投票

您可以创建一个采用通用

RuntimeException
作为参数的单一方法,并使用
if/else
语句检查异常类型并相应地修改响应。

下面的方法检查异常的类型并在 DataResponse 对象中设置适当的 HTTP 状态代码和错误消息。如果异常不是自定义异常之一,则返回通用错误消息和状态代码。

@ControllerAdvice
public class HandleExceptions {

    @ExceptionHandler(value = RuntimeException.class)
    public ResponseEntity<Object> handleCustomException(RuntimeException e) {
        HttpStatus status;
        DataResponse data;
        
        if (e instanceof CustomException1) {
            status = HttpStatus.NOT_FOUND;
            data = new DataResponse("custom status code for exception 1", e.getMessage());
        } else if (e instanceof CustomException2) {
            status = HttpStatus.NOT_FOUND;
            data = new DataResponse("custom status code for exception 2", e.getMessage());
        } else if (e instanceof CustomException3) {
            status = HttpStatus.BAD_REQUEST;
            data = new DataResponse("custom status code for exception 3", e.getMessage());
        } else {
            // Handle any other exceptions here
            status = HttpStatus.INTERNAL_SERVER_ERROR;
            data = new DataResponse("unknown error", e.getMessage());
        }
        
        ExceptionResponse exceptionResponse = new ExceptionResponse(data);
        return new ResponseEntity<>(exceptionResponse, status);
    }
}


0
投票

Surya Teja Chavali 的方法肯定有效,但为了可读性,也可以做得更好(?)。

保留多个方法,每个方法接受不同的异常,不会在代码中引入巨大的 if-else 混乱。

然后只需添加一个“普通”私有方法,然后接受

Throwable
作为参数并处理所有事情

@ControllerAdvice
public class HandleExceptions {

    @ExceptionHandler(value = CustomException1.class)
    public ResponseEntity<Object> handleCustomException1(CustomException1 e) {
        DataResponse data = new DataResponse("custom status code for exception 1", e.getMessage());
        return handleCustomException2(e, data, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(value = CustomException2.class)
    public ResponseEntity<Object> handleCustomException2(CustomException2 e) {
        DataResponse data = new DataResponse("custom status code for exception 2", e.getMessage());
        return handleException(e, data, HttpStatus.NOT_FOUND);
    }

    private ResponseEntity<ExceptionResponse> handleException(Throwable t, DataResponse data, HttpStatus status) {
        ExceptionResponse exceptionResponse = new ExceptionResponse(data);
        return new ResponseEntity<>(ExceptionResponse, status);
    }
}

您也无法使用这种方法删除所有冗余,但它会更具可读性。

© www.soinside.com 2019 - 2024. All rights reserved.