/**
* 异常的统一处理
*/
@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("服务器内部错误"));
}
}