1创建一个统一异常处理类
package com.zhdgps.znetlbs.exception;
import com.zhdgps.cloud.common.constant.Constants;
import com.zhdgps.cloud.common.exception.BusinessException;
import com.zhdgps.cloud.common.exception.EncryptException;
import com.zhdgps.cloud.common.exception.ErrorCode;
import com.zhdgps.cloud.common.http.response.ResponseHelper;
import com.zhdgps.znetlbs.dto.es.ResponseDto;
import com.zhdgps.znetlbs.remote.http.HttpGNSSResponse;
import com.zhdgps.znetlbs.remote.http.service.EncryptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.Optional;
@RestControllerAdvice
@Slf4j
public class GlobalException {
@Autowired
EncryptService encryptService;
/**
* 统一异常处理
* @param e
* @return
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> exceptionHandler(Exception e) {
log.error(Constants.HTTP_RES_CODE_500_VALUE, e);
return ResponseHelper.failed(Constants.HTTP_RES_CODE_500_VALUE);
}
/**
* 业务异常类统一处理
* @param e
* @return
*/
@ExceptionHandler(BusinessException.class)
public ResponseEntity<Object> businessExceptionHandler(BusinessException e) {
return ResponseHelper.failed(e.getErrorCode().getKey(), e.getMessage());
}
/**
* 参数绑定异常
* @param e
* @return
*/
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class, MissingServletRequestParameterException.class})
public ResponseEntity<Object> businessExceptionHandler(Exception e) {
if (e instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException exception = (MethodArgumentNotValidException) e;
String message = Optional.ofNullable(exception.getBindingResult().getFieldError())
.flatMap(fieldError -> Optional.ofNullable(fieldError.getDefaultMessage())).orElse("");
return ResponseHelper.failed(ErrorCode.MISSING_PARAMS.getKey(), StringUtils.isEmpty(message) ? "缺少必填参数" : message);
}
return ResponseHelper.failed(ErrorCode.MISSING_PARAMS.getKey(), "缺少必填参数");
}
/**
* xxx异常
* @param e
* @return
*/
@ExceptionHandler(xxxException.class)
public ResponseEntity<Object> authExceptionHandler(xxxException e) {
xxxResponse response = new xxxResponse(e.getCode() + "", e.getMessage());
return new ResponseEntity<>(xxxService.xxxhanshu(response), HttpStatus.OK);
}
@ExceptionHandler(xxxException.class)
public ResponseEntity<Object> xxxExceptionHandler(xxxException e) {
ResponseDto responseDto = new ResponseDto();
responseDto.setErrorCode(e.getErrorCode());
responseDto.setErrorMessage(e.getErrorMessage());
return new ResponseEntity<>(xxxService.xxxhanshu(responseDto), HttpStatus.OK);
}
}
2、在业务层抛出异常
if(ObjectUtils.isEmpty(receiver.getIp()) || receiver.getPort() == null){ throw new BusinessException(ErrorCode.BUSINESS_ERROR,"xxxx不存在"); }
其中ErrorCode类:
public enum ErrorCode {
/**
* 错误码不需要定义错误描述,直接抛出异常
*/
METHOD_NOT_FOUND_ERROR(404),
UNKNOWN_ERROR(500),
METHOD_NOT_SUPPORT(403),
PARAMS_INVALID(405),
DATA_NOT_EXIST(406),
DATA_DUPLICATION(407),
BUSINESS_ERROR(408),
MISSING_PARAMS(409),
PARAMS_TYPE_INVALID(402),
METHOD_ARGUMENT_NOT_VALID(400),
RECEIVER_ERROR(501),
UNAUTHORIZED(401);
private final Integer code;
ErrorCode(Integer code) {
this.code = code;
}
public Integer getKey() {
return code;
}
}
3、在controller层抛出异常,让统一异常处理类捕获,然后使用统一异常类中的方法返回提示信息
public ResponseEntity<RestResult> xxxInfo(@RequestBody @Valid xxxxVo xxxxVo, @ApiIgnore Integer userId)throws ExecutionException