iyaya

导航

异常的统一处理

/**
 * 异常的统一处理
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(自定义异常类.class)
    public ResponseEntity<CommonResponse> handleExcepting(自定义异常类 e) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .contentType(MediaType.APPLICATION_JSON)
                .body(new CommonResponse(e.getMessage()));
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<CommonResponse> handleExcepting(Exception e) {
        log.error("error", e);
        //最好是跳转到一个错误页面
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON).body(new CommonResponse("服务器内部错误"));
    }

}

posted on 2020-12-10 20:35  iyaya  阅读(89)  评论(0编辑  收藏  举报