完善统一返回 和异常处理
This commit is contained in:
@@ -3,6 +3,10 @@ package org.jiayunet.pojo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
|
||||
/**
|
||||
* 接口数据同一响应格式
|
||||
@@ -17,12 +21,31 @@ public class UnifiedResponse<T> {
|
||||
private String code;
|
||||
private String msg;
|
||||
private T data;
|
||||
private String timestamp;
|
||||
private String uuid;
|
||||
|
||||
public static <K> UnifiedResponse<K> normalResponse(K date) {
|
||||
/**
|
||||
* 设置时间戳为当前时刻(Unix秒级时间戳)
|
||||
*/
|
||||
public void setTimestamp() {
|
||||
this.timestamp = String.valueOf(Instant.now().getEpochSecond());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置uuid为当前请求链路traceId(来自MDC)
|
||||
*/
|
||||
public void setUuid() {
|
||||
this.uuid = MDC.get("traceId");
|
||||
}
|
||||
|
||||
public static <K> UnifiedResponse<K> normalResponse(K data) {
|
||||
UnifiedResponse<K> unifiedResponse = new UnifiedResponse<K>();
|
||||
unifiedResponse.setCode("0");
|
||||
unifiedResponse.setMsg("正常响应");
|
||||
unifiedResponse.setData(date);
|
||||
unifiedResponse.setData(data);
|
||||
unifiedResponse.setTimestamp();
|
||||
unifiedResponse.setUuid();
|
||||
|
||||
return unifiedResponse;
|
||||
}
|
||||
|
||||
@@ -30,6 +53,8 @@ public class UnifiedResponse<T> {
|
||||
UnifiedResponse<?> unifiedResponse = new UnifiedResponse<Object>();
|
||||
unifiedResponse.setCode(code);
|
||||
unifiedResponse.setMsg(msg);
|
||||
unifiedResponse.setTimestamp();
|
||||
unifiedResponse.setUuid();
|
||||
return unifiedResponse;
|
||||
}
|
||||
|
||||
@@ -38,6 +63,8 @@ public class UnifiedResponse<T> {
|
||||
unifiedResponse.setCode(code);
|
||||
unifiedResponse.setMsg(msg);
|
||||
unifiedResponse.setData(data);
|
||||
unifiedResponse.setTimestamp();
|
||||
unifiedResponse.setUuid();
|
||||
return unifiedResponse;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ import org.jiayunet.pojo.UnifiedResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 全局异常处理
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
@@ -26,14 +28,14 @@ public class GlobalExceptionAdvice {
|
||||
@ExceptionHandler({MethodArgumentTypeMismatchException.class})
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public UnifiedResponse<?> methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException ex) {
|
||||
ex.printStackTrace();
|
||||
return this.unifiedExpResponse(BusinessExpCodeEnum.PARAMS_INCORRECT.getCode(), ex.getMessage());
|
||||
log.error("参数类型不匹配: {}", ex.getMessage(), ex);
|
||||
return UnifiedResponse.fail(BusinessExpCodeEnum.PARAMS_INCORRECT.getCode(), ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler({BusinessException.class})
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public UnifiedResponse<?> businessCheckExceptionHandler(BusinessException ex) {
|
||||
ex.printStackTrace();
|
||||
log.warn("业务异常: code={}, msg={}", ex.getBusinessCode(), ex.getMessage());
|
||||
|
||||
String description = ex.getDescription();
|
||||
String msg = ex.getBusinessMsg();
|
||||
@@ -45,43 +47,35 @@ public class GlobalExceptionAdvice {
|
||||
}
|
||||
}
|
||||
return UnifiedResponse.fail(ex.getBusinessCode(), msg, ex.getData());
|
||||
|
||||
}
|
||||
|
||||
@ExceptionHandler({IllegalArgumentException.class})
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public UnifiedResponse<?> mybatisPlusException(IllegalArgumentException ex) {
|
||||
ex.printStackTrace();
|
||||
return this.unifiedExpResponse("断言异常", ex.getMessage());
|
||||
log.error("断言异常: {}", ex.getMessage(), ex);
|
||||
return UnifiedResponse.fail(BusinessExpCodeEnum.UNKNOWN_ERROR.getCode(), ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler({ValidationException.class})
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public UnifiedResponse<?> validationException(ValidationException ex) {
|
||||
ex.printStackTrace();
|
||||
return this.unifiedExpResponse("参数异常拦截", ex.getMessage());
|
||||
log.error("参数校验异常: {}", ex.getMessage(), ex);
|
||||
return UnifiedResponse.fail(BusinessExpCodeEnum.PARAMS_INCORRECT.getCode(), ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler({BindException.class})
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public UnifiedResponse<?> bindException(BindException ex) {
|
||||
ex.printStackTrace();
|
||||
return this.unifiedExpResponse("参数绑定异常", ex.getMessage());
|
||||
log.error("参数绑定异常: {}", ex.getMessage(), ex);
|
||||
return UnifiedResponse.fail(BusinessExpCodeEnum.PARAMS_INCORRECT.getCode(), ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler({Exception.class})
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public UnifiedResponse<?> lastExceptionHandler(Exception ex) {
|
||||
BusinessExpCodeEnum unknownError = BusinessExpCodeEnum.UNKNOWN_ERROR;
|
||||
ex.printStackTrace();
|
||||
return this.unifiedExpResponse(unknownError.getCode(), unknownError.getMsg());
|
||||
log.error("未知异常: {}", ex.getMessage(), ex);
|
||||
return UnifiedResponse.fail(unknownError.getCode(), unknownError.getMsg());
|
||||
}
|
||||
|
||||
private UnifiedResponse<?> unifiedExpResponse(String code, String msg) {
|
||||
return UnifiedResponse.fail(code, msg);
|
||||
}
|
||||
|
||||
private UnifiedResponse<?> unifiedExpResponse(String errorFrom, String code, String msg) {
|
||||
return UnifiedResponse.fail(code, msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user