统一返回数据
@Data
@NoArgsConstructor
@Accessors(chain = true)
public class Result<T> implements Serializable {
private static final long serialVersionUID = -1014694272825898751L;
private String message;
private String code;
private Boolean success;
private T data;
private String requestId;
/**
* 包含错误信息的返回值
*
* @param msg
* @return
*/
public static Result ofError(String msg) {
return of(msg, ResultEnum.ERROR.getCode(), null, false);
}
/**
* 包含错误信息的返回值
*
* @param msg
* @return
*/
public static Result ofError(String msg, String traceId) {
return of(msg, ResultEnum.ERROR.getCode(), null, false, traceId);
}
/**
* 包含错误信息的返回值
*
* @param msg
* @return
*/
public static Result ofError(String msg, String code, String traceId) {
return of(msg, code, null, false, traceId);
}
/**
* 构建一个包含正确元素的返回值
*
* @param data
* @return
*/
public static <T> Result<T> ofSuccess(T data) {
return of(null, ResultEnum.SUCCESS.getCode(), data, true);
}
public static <T> Result<T> ofSuccess(T data, String traceId) {
return of(null, ResultEnum.SUCCESS.getCode(), data, true, traceId);
}
/**
* 构建一个包含正确元素的返回值
*
* @param data
* @param code
* @return
*/
public static <T> Result<T> ofSuccess(String code, T data) {
return of(null, code, data, true);
}
/**
* 自定义一个完整的返回结构
*
* @param msg
* @param code
* @return
*/
public static <T> Result<T> of(String msg, String code, T data, Boolean success) {
Result result = new Result();
result.setMessage(msg)
.setSuccess(success)
.setData(data)
.setCode(code);
return result;
}
/**
* 自定义一个完整的返回结构
*
* @param msg
* @param code
* @return
*/
public static <T> Result<T> of(String msg, String code, T data, Boolean success, String requestId) {
Result result = of(msg, code, data, success).setRequestId(requestId);
return result;
}
}
跟踪traceId
traceId放在context中
public class MambaContext {
private final static ThreadLocal<Map> MAMBA_THREAD_LOCAL = ThreadLocal.withInitial(() -> new HashMap<String, String>(8));
private static final String TRACE_ID = "traceId";
public static void init(HttpServletRequest request) {
MAMBA_THREAD_LOCAL.remove();
initEagleEye();
}
public static void destory(HttpServletRequest request) {
MAMBA_THREAD_LOCAL.remove();
}
public static String getTraceId() {
return (String) MAMBA_THREAD_LOCAL.get().get(TRACE_ID);
}
public static void initEagleEye() {
MAMBA_THREAD_LOCAL.get().putIfAbsent(TRACE_ID,Trace.getTraceId());
}
}
使用HandlerInterceptorAdapter进行AOP处理
@Component
public class MambaInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
MambaContext.init(request);
return super.preHandle(request, response, handler);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
MambaContext.destory(request);
super.afterCompletion(request, response, handler, ex);
}
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
MambaContext.destory(request);
super.afterConcurrentHandlingStarted(request, response, handler);
}
}
通用异常处理
@RestControllerAdvice
public class ApiExceptionHandler {
/**
* 返回 Header application/json;charset=UTF-8 格式
*/
private final static MultiValueMap<String, String> EXCEPTION_HEADERS = new HttpHeaders() {{
put("Content-Type", new ArrayList<String>(1) {{
add(MediaType.APPLICATION_JSON_UTF8_VALUE);
}});
}};
@ExceptionHandler(value = Exception.class)
public ResponseEntity<Result> exceptionHandler(Exception e) {
String traceId = MambaContext.getTraceId();
log.error("TraceId: {}, errorMessage: {}, ", traceId, e.getMessage());
e.printStackTrace();
Result erroResult = null;
if (e instanceof MambaExceptionInterface) {
MambaExceptionInterface exception = (MambaExceptionInterface) e;
erroResult = Result.ofError(
exception.getResultCode().getDescription(),
exception.getResultCode().getCode(),
traceId
);
} else {
erroResult = Result.ofError(
ResultEnum.ERROR.getDescription(),
ResultEnum.ERROR.getCode(),
traceId
);
}
return new ResponseEntity<>(
erroResult,
EXCEPTION_HEADERS,
HttpStatus.OK
);
}
}
网友评论