新增 CommonResult 。
将使用手机号进行注册登陆的逻辑,进行变更~
This commit is contained in:
@@ -2,6 +2,7 @@ package cn.iocoder.common.framework.config;
|
||||
|
||||
import cn.iocoder.common.framework.constant.SysErrorCodeEnum;
|
||||
import cn.iocoder.common.framework.exception.ServiceException;
|
||||
import cn.iocoder.common.framework.util.ExceptionUtil;
|
||||
import cn.iocoder.common.framework.vo.RestResult;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
@@ -9,7 +10,6 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
|
||||
@ControllerAdvice
|
||||
@@ -21,19 +21,13 @@ public class GlobalExceptionHandler {
|
||||
return RestResult.error(ex.getCode(), ex.getMessage());
|
||||
}
|
||||
|
||||
// 处理 Spring 动态代理调用时,发生 UndeclaredThrowableException 的情况。
|
||||
// 不了解的胖友,可以看看 https://segmentfault.com/a/1190000012262244 文章
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = UndeclaredThrowableException.class)
|
||||
public RestResult undeclaredThrowableExceptionHandler(HttpServletRequest req, UndeclaredThrowableException e) {
|
||||
// 尝试获得 ServiceException 异常。如果是,则使用 serviceExceptionHandler 方法处理。
|
||||
Throwable undeclaredThrowable = e.getUndeclaredThrowable();
|
||||
if (undeclaredThrowable instanceof InvocationTargetException) {
|
||||
InvocationTargetException invocationTargetException = (InvocationTargetException) undeclaredThrowable;
|
||||
Throwable targetException = invocationTargetException.getTargetException();
|
||||
if (targetException != null & targetException instanceof ServiceException) {
|
||||
return serviceExceptionHandler(req, (ServiceException) targetException);
|
||||
}
|
||||
ServiceException serviceException = ExceptionUtil.getServiceException(e);
|
||||
if (serviceException != null) {
|
||||
return serviceExceptionHandler(req, serviceException);
|
||||
}
|
||||
// 获得不到,使用 异常日志 方法处理。
|
||||
return resultExceptionHandler(req, e);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.iocoder.common.framework.config;
|
||||
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.common.framework.vo.RestResult;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -21,6 +22,9 @@ public class GlobalResponseBodyAdvice implements ResponseBodyAdvice {
|
||||
if (body instanceof RestResult) {
|
||||
return body;
|
||||
}
|
||||
if (body instanceof CommonResult) { // TODO 芋艿,后续要改下
|
||||
return body;
|
||||
}
|
||||
return RestResult.ok(body);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package cn.iocoder.common.framework.util;
|
||||
|
||||
import cn.iocoder.common.framework.exception.ServiceException;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
|
||||
public class ExceptionUtil {
|
||||
|
||||
public static ServiceException getServiceException(Exception e) {
|
||||
if (e instanceof UndeclaredThrowableException) {
|
||||
return getServiceException((UndeclaredThrowableException) e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 处理 Spring 动态代理调用时,发生 UndeclaredThrowableException 的情况。
|
||||
// 不了解的胖友,可以先看看 https://segmentfault.com/a/1190000012262244 文章
|
||||
// 原因是:
|
||||
// 1. Dubbo 动态代理 Wrapper 会将抛出的异常,包装成 InvocationTargetException 异常
|
||||
// 2. Spring AOP 发现是 InvocationTargetException 异常是非方法定义的异常,则会包装成 UndeclaredThrowableException 异常。
|
||||
public static ServiceException getServiceException(UndeclaredThrowableException e) {
|
||||
Throwable undeclaredThrowable = e.getUndeclaredThrowable();
|
||||
if (undeclaredThrowable instanceof InvocationTargetException) {
|
||||
InvocationTargetException invocationTargetException = (InvocationTargetException) undeclaredThrowable;
|
||||
Throwable targetException = invocationTargetException.getTargetException();
|
||||
if (targetException != null & targetException instanceof ServiceException) {
|
||||
return (ServiceException) targetException;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package cn.iocoder.common.framework.util;
|
||||
|
||||
import cn.iocoder.common.framework.exception.ServiceException;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -38,6 +39,16 @@ public class ServiceExceptionUtil {
|
||||
ServiceExceptionUtil.messages.put(code, message);
|
||||
}
|
||||
|
||||
// TODO 芋艿,可能不是目前最优解,目前暂时这样
|
||||
public static <T> CommonResult<T> error(Integer code) {
|
||||
return CommonResult.error(code, messages.get(code));
|
||||
}
|
||||
|
||||
public static CommonResult error(Integer code, Object... params) {
|
||||
String message = doFormat(code, messages.get(code), params);
|
||||
return CommonResult.error(code, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建指定编号的 ServiceException 的异常
|
||||
*
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package cn.iocoder.common.framework.vo;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class CommonResult<T> {
|
||||
|
||||
public static Integer CODE_SUCCESS = 0;
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*/
|
||||
private Integer code;
|
||||
/**
|
||||
* 错误提示
|
||||
*/
|
||||
private String message;
|
||||
/**
|
||||
* 返回数据
|
||||
*/
|
||||
private T data;
|
||||
|
||||
/**
|
||||
* 将传入的 result 对象,转换成另外一个泛型结果的对象
|
||||
*
|
||||
* 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。
|
||||
*
|
||||
* @param result 传入的 result 对象
|
||||
* @param <T> 返回的泛型
|
||||
* @return 新的 CommonResult 对象
|
||||
*/
|
||||
public static <T> CommonResult<T> error(CommonResult<?> result) {
|
||||
return error(result.getCode(), result.getMessage());
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> error(Integer code, String message) {
|
||||
Assert.isTrue(!CODE_SUCCESS.equals(code), "code 必须是错误的!");
|
||||
CommonResult<T> result = new CommonResult<>();
|
||||
result.code = code;
|
||||
result.message = message;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> success(T data) {
|
||||
CommonResult<T> result = new CommonResult<>();
|
||||
result.code = CODE_SUCCESS;
|
||||
result.data = data;
|
||||
result.message = "";
|
||||
return result;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return CODE_SUCCESS.equals(code);
|
||||
}
|
||||
|
||||
public boolean isError() {
|
||||
return !isSuccess();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -53,4 +53,5 @@ public class RestResult {
|
||||
public void setData(Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user