From 085c22f04ee14e06d6cd7e46dc2acef680d79207 Mon Sep 17 00:00:00 2001 From: wangkang Date: Fri, 12 May 2017 13:34:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=94=99=E8=AF=AF=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/egridcloud/udf/core/RestResponse.java | 182 ++++++++---- .../udf/core/exception/ErrorMessage.java | 277 ------------------ .../udf/core/exception/ErrorResult.java | 122 ++++++++ .../udf/core/exception/HttpErrorMessage.java | 183 ------------ .../exception/NoResourceFoundMessage.java | 107 ------- .../exception/ParameterValidErrorMessage.java | 59 ---- .../exception/handle/ExceptionMapping.java | 165 +++-------- 7 files changed, 287 insertions(+), 808 deletions(-) delete mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorMessage.java create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorResult.java delete mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/HttpErrorMessage.java delete mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/NoResourceFoundMessage.java delete mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ParameterValidErrorMessage.java diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/RestResponse.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/RestResponse.java index c044e2d..1107836 100644 --- a/udf-starter-core/src/main/java/com/egridcloud/udf/core/RestResponse.java +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/RestResponse.java @@ -7,6 +7,10 @@ package com.egridcloud.udf.core; import java.io.Serializable; +import java.util.UUID; + +import com.egridcloud.udf.core.exception.ErrorCode; +import com.egridcloud.udf.core.exception.ErrorResult; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -33,18 +37,10 @@ public class RestResponse implements Serializable { private static final long serialVersionUID = 1L; /** - * - * 描述 : 默认状态码 - * + * 描述 : 响应ID */ - private static final String DEF_RETURN_CODE = "SUCCESS"; - - /** - * - * 描述 : 默认状态码描述 - * - */ - private static final String DEF_RETURN_MESSAGE = "操作成功"; + @ApiModelProperty(value = "响应ID", required = true, dataType = "string") + private String id = UUID.randomUUID().toString(); /** * @@ -52,7 +48,7 @@ public class RestResponse implements Serializable { * */ @ApiModelProperty(value = "状态码(业务定义)", required = true, dataType = "string") - private String returnCode = DEF_RETURN_CODE; + private String code = "SUCCESS"; /** * @@ -60,7 +56,7 @@ public class RestResponse implements Serializable { * */ @ApiModelProperty(value = "状态码描述(业务定义)", required = true, dataType = "string") - private String returnMessage = DEF_RETURN_MESSAGE; + private String message = "操作成功"; /** * @@ -68,7 +64,13 @@ public class RestResponse implements Serializable { * */ @ApiModelProperty(value = "结果集(泛型)", required = true, dataType = "object") - private T result; //NOSONAR + private T result = null; //NOSONAR + + /** + * 描述 : 错误 + */ + @ApiModelProperty(value = "错误", required = true, dataType = "object") + private ErrorResult error = null; /** * @@ -95,102 +97,164 @@ public class RestResponse implements Serializable { this.result = result; } + /** + * + * 描述 : 构造函数 + * + * @param errorCode 错误码 + * + * @param error 错误 + * + */ + public RestResponse(ErrorCode errorCode, ErrorResult error) { + super(); + this.code = errorCode.name(); + this.message = errorCode.value(); + this.error = error; + } + + /** + * + * 描述 : 构造函数 + * + * @param code 状态码(业务定义) + * + * @param message 状态码描述(业务定义) + * + * @param error 错误 + * + */ + public RestResponse(String code, String message, ErrorResult error) { + super(); + this.code = code; + this.message = message; + this.error = error; + } + + /** + * + * 描述 : 构造函数 + * + * @param code 状态码(业务定义) + * + * @param message 状态码描述(业务定义) + * + * + */ + public RestResponse(String code, String message) { + super(); + this.code = code; + this.message = message; + } + /** * * 描述 : 构造函数 * * * - * @param returnCode 状态码(业务定义) + * @param code 状态码(业务定义) * - * @param returnMessage 状态码描述(业务定义) + * @param message 状态码描述(业务定义) * * @param result 结果集(泛型) * */ - public RestResponse(String returnCode, String returnMessage, T result) { + public RestResponse(String code, String message, T result) { super(); - this.returnCode = returnCode; - this.returnMessage = returnMessage; + this.code = code; + this.message = message; this.result = result; } /** - * - * 描述 : 获取returnCode + * 描述 : 获取id * - * - * - * @return the returnCode - * + * @return the id */ - public String getReturnCode() { - return returnCode; + public String getId() { + return id; } /** - * - * 描述 : 设置returnCode + * 描述 : 设置id * - * - * - * @param returnCode the returnCode to set - * + * @param id the id to set */ - public void setReturnCode(String returnCode) { - this.returnCode = returnCode; + public void setId(String id) { + this.id = id; } /** - * - * 描述 : 获取returnMessage + * 描述 : 获取code * - * - * - * @return the returnMessage - * + * @return the code */ - public String getReturnMessage() { - return returnMessage; + public String getCode() { + return code; } /** - * - * 描述 : 设置returnMessage + * 描述 : 设置code * - * - * - * @param returnMessage the returnMessage to set - * + * @param code the code to set */ - public void setReturnMessage(String returnMessage) { - this.returnMessage = returnMessage; + public void setCode(String code) { + this.code = code; + } + + /** + * 描述 : 获取message + * + * @return the message + */ + public String getMessage() { + return message; + } + + /** + * 描述 : 设置message + * + * @param message the message to set + */ + public void setMessage(String message) { + this.message = message; } /** - * * 描述 : 获取result * - * - * * @return the result - * */ public T getResult() { return result; } /** - * * 描述 : 设置result * - * - * * @param result the result to set - * */ public void setResult(T result) { this.result = result; } + /** + * 描述 : 获取error + * + * @return the error + */ + public ErrorResult getError() { + return error; + } + + /** + * 描述 : 设置error + * + * @param error the error to set + */ + public void setError(ErrorResult error) { + this.error = error; + } + } diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorMessage.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorMessage.java deleted file mode 100644 index 4b30b1e..0000000 --- a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorMessage.java +++ /dev/null @@ -1,277 +0,0 @@ -/** - * ErrorMessage.java - * Created at 2016-09-27 - * Created by wangkang - * Copyright (C) 2016 egridcloud.com, All rights reserved. - */ -package com.egridcloud.udf.core.exception; - -import java.io.Serializable; -import java.util.Date; - -/** - * 描述 : 错误信息 - * - * @author wangkang - * - */ -public class ErrorMessage implements Serializable { - /** - * 描述 : id - */ - private static final long serialVersionUID = 1L; - - /** - * 描述 : 错误ID - */ - private String id; - - /** - * 描述 : http状态 - */ - private int httpStatus; - - /** - * 描述 : 错误代码 - */ - private String errorCode; - - /** - * 描述 : 错误描述 - */ - private String errorMsg; - - /** - * 描述 : 错误发生时间 - */ - private Date errorDate; - - /** - * 描述 : 异常类名 - */ - private String exceptionType; - - /** - * 描述 : 异常信息 - */ - private String exceptionMessage; - - /** - * 描述 : 详细异常堆栈信息 - */ - private String exceptionStackTrace; - - /** - * 描述 : 资源部不在错误信息 - */ - private NoResourceFoundMessage noResourceFoundMessage; - - /** - * 描述 : 参数校验错误信息 - */ - private ParameterValidErrorMessage parameterValidErrorMessage; - - /** - * 描述 : http服务错误 - */ - private HttpErrorMessage httpErrorMessage; - - /** - * 描述 : 获取httpErrorMessage - * - * @return the httpErrorMessage - */ - public HttpErrorMessage getHttpErrorMessage() { - return httpErrorMessage; - } - - /** - * 描述 : 设置httpErrorMessage - * - * @param httpErrorMessage the httpErrorMessage to set - */ - public void setHttpErrorMessage(HttpErrorMessage httpErrorMessage) { - this.httpErrorMessage = httpErrorMessage; - } - - /** - * 描述 : 获取noResourceFoundMessage - * - * @return the noResourceFoundMessage - */ - public NoResourceFoundMessage getNoResourceFoundMessage() { - return noResourceFoundMessage; - } - - /** - * 描述 : 设置noResourceFoundMessage - * - * @param noResourceFoundMessage the noResourceFoundMessage to set - */ - public void setNoResourceFoundMessage(NoResourceFoundMessage noResourceFoundMessage) { - this.noResourceFoundMessage = noResourceFoundMessage; - } - - /** - * 描述 : 获取parameterValidErrorMessage - * - * @return the parameterValidErrorMessage - */ - public ParameterValidErrorMessage getParameterValidErrorMessage() { - return parameterValidErrorMessage; - } - - /** - * 描述 : 设置parameterValidErrorMessage - * - * @param parameterValidErrorMessage the parameterValidErrorMessage to set - */ - public void setParameterValidErrorMessage(ParameterValidErrorMessage parameterValidErrorMessage) { - this.parameterValidErrorMessage = parameterValidErrorMessage; - } - - /** - * 描述 : 获取id - * - * @return the id - */ - public String getId() { - return id; - } - - /** - * 描述 : 设置id - * - * @param id the id to set - */ - public void setId(String id) { - this.id = id; - } - - /** - * 描述 : 获取errorDate - * - * @return the errorDate - */ - public Date getErrorDate() { - return errorDate; - } - - /** - * 描述 : 设置errorDate - * - * @param errorDate the errorDate to set - */ - public void setErrorDate(Date errorDate) { - this.errorDate = errorDate; - } - - /** - * 描述 : 获取httpStatus - * - * @return the httpStatus - */ - public int getHttpStatus() { - return httpStatus; - } - - /** - * 描述 : 设置httpStatus - * - * @param httpStatus the httpStatus to set - */ - public void setHttpStatus(int httpStatus) { - this.httpStatus = httpStatus; - } - - /** - * 描述 : 获取errorCode - * - * @return the errorCode - */ - public String getErrorCode() { - return errorCode; - } - - /** - * 描述 : 设置errorCode - * - * @param errorCode the errorCode to set - */ - public void setErrorCode(String errorCode) { - this.errorCode = errorCode; - } - - /** - * 描述 : 获取errorMsg - * - * @return the errorMsg - */ - public String getErrorMsg() { - return errorMsg; - } - - /** - * 描述 : 设置errorMsg - * - * @param errorMsg the errorMsg to set - */ - public void setErrorMsg(String errorMsg) { - this.errorMsg = errorMsg; - } - - /** - * 描述 : 获取exceptionType - * - * @return the exceptionType - */ - public String getExceptionType() { - return exceptionType; - } - - /** - * 描述 : 设置exceptionType - * - * @param exceptionType the exceptionType to set - */ - public void setExceptionType(String exceptionType) { - this.exceptionType = exceptionType; - } - - /** - * 描述 : 获取exceptionMessage - * - * @return the exceptionMessage - */ - public String getExceptionMessage() { - return exceptionMessage; - } - - /** - * 描述 : 设置exceptionMessage - * - * @param exceptionMessage the exceptionMessage to set - */ - public void setExceptionMessage(String exceptionMessage) { - this.exceptionMessage = exceptionMessage; - } - - /** - * 描述 : 获取exceptionStackTrace - * - * @return the exceptionStackTrace - */ - public String getExceptionStackTrace() { - return exceptionStackTrace; - } - - /** - * 描述 : 设置exceptionStackTrace - * - * @param exceptionStackTrace the exceptionStackTrace to set - */ - public void setExceptionStackTrace(String exceptionStackTrace) { - this.exceptionStackTrace = exceptionStackTrace; - } - -} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorResult.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorResult.java new file mode 100644 index 0000000..7b05e9e --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorResult.java @@ -0,0 +1,122 @@ +/** + * ErrorResult.java + * Created at 2016-09-27 + * Created by wangkang + * Copyright (C) 2016 egridcloud.com, All rights reserved. + */ +package com.egridcloud.udf.core.exception; + +import java.io.Serializable; +import java.util.Date; + +import io.swagger.annotations.ApiModelProperty; + +/** + * 描述 : 错误信息 + * + * @author wangkang + * + */ +public class ErrorResult implements Serializable { + /** + * 描述 : id + */ + private static final long serialVersionUID = 1L; + + /** + * 描述 : 异常时间 + */ + @ApiModelProperty(value = "异常时间", required = true, dataType = "date") + private Date exceptionDate; + + /** + * 描述 : 异常类名 + */ + @ApiModelProperty(value = "异常类名", required = true, dataType = "string") + private String exceptionType; + + /** + * 描述 : 异常信息 + */ + @ApiModelProperty(value = "异常信息", required = true, dataType = "string") + private String exceptionMessage; + + /** + * 描述 : 详细异常堆栈信息 + */ + @ApiModelProperty(value = "异常堆栈", required = true, dataType = "string") + private String exceptionStackTrace; + + /** + * 描述 : 获取exceptionDate + * + * @return the exceptionDate + */ + public Date getExceptionDate() { + return exceptionDate; + } + + /** + * 描述 : 设置exceptionDate + * + * @param exceptionDate the exceptionDate to set + */ + public void setExceptionDate(Date exceptionDate) { + this.exceptionDate = exceptionDate; + } + + /** + * 描述 : 获取exceptionType + * + * @return the exceptionType + */ + public String getExceptionType() { + return exceptionType; + } + + /** + * 描述 : 设置exceptionType + * + * @param exceptionType the exceptionType to set + */ + public void setExceptionType(String exceptionType) { + this.exceptionType = exceptionType; + } + + /** + * 描述 : 获取exceptionMessage + * + * @return the exceptionMessage + */ + public String getExceptionMessage() { + return exceptionMessage; + } + + /** + * 描述 : 设置exceptionMessage + * + * @param exceptionMessage the exceptionMessage to set + */ + public void setExceptionMessage(String exceptionMessage) { + this.exceptionMessage = exceptionMessage; + } + + /** + * 描述 : 获取exceptionStackTrace + * + * @return the exceptionStackTrace + */ + public String getExceptionStackTrace() { + return exceptionStackTrace; + } + + /** + * 描述 : 设置exceptionStackTrace + * + * @param exceptionStackTrace the exceptionStackTrace to set + */ + public void setExceptionStackTrace(String exceptionStackTrace) { + this.exceptionStackTrace = exceptionStackTrace; + } + +} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/HttpErrorMessage.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/HttpErrorMessage.java deleted file mode 100644 index 1a08168..0000000 --- a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/HttpErrorMessage.java +++ /dev/null @@ -1,183 +0,0 @@ -/** - * HttpErrorMessage.java - * Created at 2016-11-05 - * Created by wangkang - * Copyright (C) 2016 egridcloud.com, All rights reserved. - */ -package com.egridcloud.udf.core.exception; - -import java.io.IOException; -import java.io.Serializable; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.web.client.RestClientResponseException; - -import com.egridcloud.udf.core.RestResponse; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.databind.JavaType; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -/** - * 描述 : HttpServerErrorMessage - * - * @author wangkang - * - */ -public class HttpErrorMessage implements Serializable { - - /** - * 描述 : 日志 - */ - private static final Logger LOGGER = LoggerFactory.getLogger(HttpErrorMessage.class); - - /** - * 描述 : ID - */ - private static final long serialVersionUID = 1L; - - /** - * 描述 : rawStatusCode - */ - private int rawStatusCode; - - /** - * 描述 : statusText - */ - private String statusText; - - /** - * 描述 : responseBody - */ - private RestResponse responseBody; - - /** - * 描述 : responseHeaders - */ - private HttpHeaders responseHeaders; - - /** - * 描述 : statusCode - */ - private HttpStatus statusCode; - - /** - * 描述 : 构造函数 - * - * @param restClientResponseException restClientResponseException - * @param xssObjectMapper xssObjectMapper - */ - public HttpErrorMessage(RestClientResponseException restClientResponseException, - ObjectMapper xssObjectMapper) { - rawStatusCode = restClientResponseException.getRawStatusCode(); - responseHeaders = restClientResponseException.getResponseHeaders(); - statusCode = HttpStatus.valueOf(rawStatusCode); - statusText = restClientResponseException.getStatusText(); - try { - JavaType javaType = xssObjectMapper.getTypeFactory() - .constructParametricType(RestResponse.class, ErrorMessage.class); - responseBody = xssObjectMapper - .readValue(restClientResponseException.getResponseBodyAsString(), javaType); - } catch (JsonParseException e) { - LOGGER.error("JsonParseException:", e); - } catch (JsonMappingException e1) { - LOGGER.error("JsonMappingException:", e1); - } catch (IOException e2) { - LOGGER.error("IOException:", e2); - } - } - - /** - * 描述 : 获取responseBody - * - * @return the responseBody - */ - public RestResponse getResponseBody() { - return responseBody; - } - - /** - * 描述 : 设置responseBody - * - * @param responseBody the responseBody to set - */ - public void setResponseBody(RestResponse responseBody) { - this.responseBody = responseBody; - } - - /** - * 描述 : 获取rawStatusCode - * - * @return the rawStatusCode - */ - public int getRawStatusCode() { - return rawStatusCode; - } - - /** - * 描述 : 设置rawStatusCode - * - * @param rawStatusCode the rawStatusCode to set - */ - public void setRawStatusCode(int rawStatusCode) { - this.rawStatusCode = rawStatusCode; - } - - /** - * 描述 : 获取statusText - * - * @return the statusText - */ - public String getStatusText() { - return statusText; - } - - /** - * 描述 : 设置statusText - * - * @param statusText the statusText to set - */ - public void setStatusText(String statusText) { - this.statusText = statusText; - } - - /** - * 描述 : 获取responseHeaders - * - * @return the responseHeaders - */ - public HttpHeaders getResponseHeaders() { - return responseHeaders; - } - - /** - * 描述 : 设置responseHeaders - * - * @param responseHeaders the responseHeaders to set - */ - public void setResponseHeaders(HttpHeaders responseHeaders) { - this.responseHeaders = responseHeaders; - } - - /** - * 描述 : 获取statusCode - * - * @return the statusCode - */ - public HttpStatus getStatusCode() { - return statusCode; - } - - /** - * 描述 : 设置statusCode - * - * @param statusCode the statusCode to set - */ - public void setStatusCode(HttpStatus statusCode) { - this.statusCode = statusCode; - } - -} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/NoResourceFoundMessage.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/NoResourceFoundMessage.java deleted file mode 100644 index f107b7a..0000000 --- a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/NoResourceFoundMessage.java +++ /dev/null @@ -1,107 +0,0 @@ -/** - * NoResourceFoundMessage.java - * Created at 2016-11-05 - * Created by wangkang - * Copyright (C) 2016 egridcloud.com, All rights reserved. - */ -package com.egridcloud.udf.core.exception; - -import java.io.Serializable; - -import org.springframework.http.HttpHeaders; -import org.springframework.web.servlet.NoHandlerFoundException; - -/** - * 描述 : NoResourceFoundMessage - * - * @author wangkang - * - */ -public class NoResourceFoundMessage implements Serializable { - - /** - * 描述 : ID - */ - private static final long serialVersionUID = 1L; - - /** - * 描述 : httpMethod - */ - private String httpMethod; - - /** - * 描述 : requestURL - */ - private String requestUrl; - - /** - * 描述 : headers - */ - private HttpHeaders headers; - - /** - * 描述 : 构造函数 - * - * @param noHandlerFoundException noHandlerFoundException - */ - public NoResourceFoundMessage(NoHandlerFoundException noHandlerFoundException) { - this.headers = noHandlerFoundException.getHeaders(); - this.httpMethod = noHandlerFoundException.getHttpMethod(); - this.requestUrl = noHandlerFoundException.getRequestURL(); - } - - /** - * 描述 : 获取httpMethod - * - * @return the httpMethod - */ - public String getHttpMethod() { - return httpMethod; - } - - /** - * 描述 : 设置httpMethod - * - * @param httpMethod the httpMethod to set - */ - public void setHttpMethod(String httpMethod) { - this.httpMethod = httpMethod; - } - - /** - * 描述 : 获取requestUrl - * - * @return the requestUrl - */ - public String getRequestUrl() { - return requestUrl; - } - - /** - * 描述 : 设置requestUrl - * - * @param requestUrl the requestUrl to set - */ - public void setRequestUrl(String requestUrl) { - this.requestUrl = requestUrl; - } - - /** - * 描述 : 获取headers - * - * @return the headers - */ - public HttpHeaders getHeaders() { - return headers; - } - - /** - * 描述 : 设置headers - * - * @param headers the headers to set - */ - public void setHeaders(HttpHeaders headers) { - this.headers = headers; - } - -} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ParameterValidErrorMessage.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ParameterValidErrorMessage.java deleted file mode 100644 index d0348b8..0000000 --- a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ParameterValidErrorMessage.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * ParameterValidErrorMessage.java - * Created at 2016-11-05 - * Created by wangkang - * Copyright (C) 2016 egridcloud.com, All rights reserved. - */ -package com.egridcloud.udf.core.exception; - -import java.io.Serializable; -import java.util.List; - -import org.springframework.validation.ObjectError; - -/** - * 描述 : ParameterValidErrorMessage - * - * @author wangkang - * - */ -public class ParameterValidErrorMessage implements Serializable { - - /** - * 描述 : ID - */ - private static final long serialVersionUID = 1L; - - /** - * 描述 : 参数静态校验错误信息 - */ - private List allErrors; - - /** - * 描述 : 构造函数 - * - * @param parameterValidException parameterValidException - */ - public ParameterValidErrorMessage(ParameterValidException parameterValidException) { - allErrors = parameterValidException.getAllErrors(); - } - - /** - * 描述 : 获取allErrors - * - * @return the allErrors - */ - public List getAllErrors() { - return allErrors; - } - - /** - * 描述 : 设置allErrors - * - * @param allErrors the allErrors to set - */ - public void setAllErrors(List allErrors) { - this.allErrors = allErrors; - } - -} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/handle/ExceptionMapping.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/handle/ExceptionMapping.java index 7f90a48..e570a42 100644 --- a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/handle/ExceptionMapping.java +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/handle/ExceptionMapping.java @@ -7,7 +7,6 @@ package com.egridcloud.udf.core.exception.handle; import java.util.Date; -import java.util.UUID; import org.apache.commons.lang3.exception.ExceptionUtils; import org.slf4j.Logger; @@ -26,15 +25,11 @@ import com.egridcloud.udf.core.ApplicationConfig; import com.egridcloud.udf.core.RestResponse; import com.egridcloud.udf.core.exception.AuthException; import com.egridcloud.udf.core.exception.ErrorCode; -import com.egridcloud.udf.core.exception.ErrorMessage; -import com.egridcloud.udf.core.exception.HttpErrorMessage; -import com.egridcloud.udf.core.exception.NoResourceFoundMessage; -import com.egridcloud.udf.core.exception.ParameterValidErrorMessage; +import com.egridcloud.udf.core.exception.ErrorResult; import com.egridcloud.udf.core.exception.ParameterValidException; import com.egridcloud.udf.core.exception.PermissionException; import com.egridcloud.udf.core.exception.SystemException; import com.egridcloud.udf.core.exception.SystemRuntimeException; -import com.fasterxml.jackson.databind.ObjectMapper; /** * 描述 : 全局异常处理 @@ -56,12 +51,6 @@ public class ExceptionMapping { @Autowired private ApplicationConfig applicationConfig; - /** - * 描述 : json解析器 - */ - @Autowired - private ObjectMapper xssObjectMapper; - /** * 描述 : 构造函数 * @@ -73,157 +62,106 @@ public class ExceptionMapping { /** * 描述 : 捕获PermissionException异常(权限异常) * - * @param permissionException 异常 + * @param exception 异常 * @return 错误信息 */ @ExceptionHandler(value = PermissionException.class) @ResponseStatus(HttpStatus.FORBIDDEN) @ResponseBody - public RestResponse permissionException(PermissionException permissionException) { - String returnCode = ErrorCode.PERMISSION_ERROR.name(); - String returnMessage = ErrorCode.PERMISSION_ERROR.value(); - ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.FORBIDDEN.value(), returnCode, - returnMessage, applicationConfig.isOutputExceptionStackTrace(), permissionException); - LOGGER.error(em.getId(), permissionException); - return new RestResponse<>(returnCode, returnMessage, em); + public RestResponse permissionException(PermissionException exception) { + return new RestResponse<>(ErrorCode.PERMISSION_ERROR, buildError(exception)); } /** * 描述 : 捕获AuthException异常(认证异常) * - * @param authException 异常 + * @param exception 异常 * @return 错误信息 */ @ExceptionHandler(value = AuthException.class) @ResponseStatus(HttpStatus.UNAUTHORIZED) @ResponseBody - public RestResponse authException(AuthException authException) { - String returnCode = ErrorCode.AUTH_ERROR.name(); - String returnMessage = ErrorCode.AUTH_ERROR.value(); - ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.UNAUTHORIZED.value(), - returnCode, returnMessage, applicationConfig.isOutputExceptionStackTrace(), authException); - LOGGER.error(em.getId(), authException); - return new RestResponse<>(returnCode, returnMessage, em); + public RestResponse authException(AuthException exception) { + return new RestResponse<>(ErrorCode.AUTH_ERROR, buildError(exception)); } /** * 描述 : 捕获ParameterValidException异常(参数校验错误) * - * @param parameterValidException 异常 + * @param exception 异常 * @return 错误信息 */ @ExceptionHandler(value = ParameterValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseBody - public RestResponse parameterValidException( - ParameterValidException parameterValidException) { - String returnCode = ErrorCode.PARAMETER_VALID_ERROR.name(); - String returnMessage = ErrorCode.PARAMETER_VALID_ERROR.value(); - ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.BAD_REQUEST.value(), returnCode, - returnMessage, applicationConfig.isOutputExceptionStackTrace(), parameterValidException); - em.setParameterValidErrorMessage(new ParameterValidErrorMessage(parameterValidException)); - LOGGER.error(em.getId(), parameterValidException); - return new RestResponse<>(returnCode, returnMessage, em); + public RestResponse parameterValidException(ParameterValidException exception) { + return new RestResponse<>(ErrorCode.PARAMETER_VALID_ERROR, buildError(exception)); } /** * 描述 : 捕获RestClientResponseException异常(http服务异常) * - * @param restClientResponseException 异常 + * @param exception 异常 * @return 错误信息 */ @ExceptionHandler(value = RestClientResponseException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseBody - public RestResponse restClientResponseException( - RestClientResponseException restClientResponseException) { - String returnCode = ErrorCode.HTTP_ERROR.name(); - String returnMessage = ErrorCode.HTTP_ERROR.value(); - ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), - returnCode, returnMessage, applicationConfig.isOutputExceptionStackTrace(), - restClientResponseException); - em.setHttpErrorMessage(new HttpErrorMessage(restClientResponseException, xssObjectMapper)); - LOGGER.error(em.getId(), restClientResponseException); - return new RestResponse<>(returnCode, returnMessage, em); + public RestResponse restClientResponseException(RestClientResponseException exception) { + return new RestResponse<>(ErrorCode.HTTP_ERROR, buildError(exception)); } /** * 描述 : 捕获NoHandlerFoundException异常(资源不存在) * - * @param noHandlerFoundException 异常 + * @param exception 异常 * @return 错误信息 */ @ExceptionHandler(value = NoHandlerFoundException.class) @ResponseStatus(HttpStatus.NOT_FOUND) @ResponseBody - public RestResponse noHandlerFoundException( - NoHandlerFoundException noHandlerFoundException) { - String returnCode = ErrorCode.NO_RESOURCE_FOUND.name(); - String returnMessage = ErrorCode.NO_RESOURCE_FOUND.value(); - ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.NOT_FOUND.value(), returnCode, - returnMessage, applicationConfig.isOutputExceptionStackTrace(), noHandlerFoundException); - em.setNoResourceFoundMessage(new NoResourceFoundMessage(noHandlerFoundException)); - LOGGER.error(em.getId(), noHandlerFoundException); - return new RestResponse<>(returnCode, returnMessage, em); + public RestResponse noHandlerFoundException(NoHandlerFoundException exception) { + return new RestResponse<>(ErrorCode.NO_RESOURCE_FOUND, buildError(exception)); } /** * 描述 : 捕获httpRequestMethodNotSupportedException异常(方法不允许) * - * @param httpRequestMethodNotSupportedException 异常 + * @param exception 异常 * @return 错误信息 */ @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) @ResponseBody - public RestResponse httpRequestMethodNotSupportedException( - HttpRequestMethodNotSupportedException httpRequestMethodNotSupportedException) { - String returnCode = ErrorCode.METHOD_NOT_ALLOWED.name(); - String returnMessage = ErrorCode.METHOD_NOT_ALLOWED.value(); - ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.METHOD_NOT_ALLOWED.value(), - returnCode, returnMessage, applicationConfig.isOutputExceptionStackTrace(), - httpRequestMethodNotSupportedException); - LOGGER.error(em.getId(), httpRequestMethodNotSupportedException); - return new RestResponse<>(returnCode, returnMessage, em); + public RestResponse httpRequestMethodNotSupportedException( + HttpRequestMethodNotSupportedException exception) { + return new RestResponse<>(ErrorCode.METHOD_NOT_ALLOWED, buildError(exception)); } /** * 描述 : 捕获SystemException异常(系统检查型异常) * - * @param systemException 异常 + * @param exception 异常 * @return 错误信息 */ @ExceptionHandler(value = SystemException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseBody - public RestResponse systemException(SystemException systemException) { - String returnCode = ErrorCode.ERROR.name(); - String returnMessage = ErrorCode.ERROR.value(); - ErrorMessage em = - ExceptionMapping.buildErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), returnCode, - returnMessage, applicationConfig.isOutputExceptionStackTrace(), systemException); - LOGGER.error(em.getId(), systemException); - return new RestResponse<>(returnCode, returnMessage, em); + public RestResponse systemException(SystemException exception) { + return new RestResponse<>(ErrorCode.ERROR, buildError(exception)); } /** * 描述 : 捕获SystemRuntimeException异常(系统运行时型异常) * - * @param systemRuntimeException 异常 + * @param exception 异常 * @return 错误信息 */ @ExceptionHandler(value = SystemRuntimeException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseBody - public RestResponse systemRuntimeException( - SystemRuntimeException systemRuntimeException) { - String returnCode = ErrorCode.ERROR.name(); - String returnMessage = ErrorCode.ERROR.value(); - ErrorMessage em = - ExceptionMapping.buildErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), returnCode, - returnMessage, applicationConfig.isOutputExceptionStackTrace(), systemRuntimeException); - LOGGER.error(em.getId(), systemRuntimeException); - return new RestResponse<>(returnCode, returnMessage, em); + public RestResponse systemRuntimeException(SystemRuntimeException exception) { + return new RestResponse<>(ErrorCode.ERROR, buildError(exception)); } /** @@ -235,58 +173,39 @@ public class ExceptionMapping { @ExceptionHandler(value = Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseBody - public RestResponse exception(Exception exception) { - String returnCode = ErrorCode.ERROR.name(); - String returnMessage = ErrorCode.ERROR.value(); - ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), - returnCode, returnMessage, applicationConfig.isOutputExceptionStackTrace(), exception); - LOGGER.error(em.getId(), exception); - return new RestResponse<>(returnCode, returnMessage, em); + public RestResponse exception(Exception exception) { + return new RestResponse<>(ErrorCode.ERROR, buildError(exception)); } /** * 描述 : 捕获RuntimeException异常(运行时异常) * - * @param runtimeException 异常 + * @param exception 异常 * @return 错误信息 */ @ExceptionHandler(value = RuntimeException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseBody - public RestResponse runtimeException(RuntimeException runtimeException) { - String returnCode = ErrorCode.ERROR.name(); - String returnMessage = ErrorCode.ERROR.value(); - ErrorMessage em = - ExceptionMapping.buildErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), returnCode, - returnMessage, applicationConfig.isOutputExceptionStackTrace(), runtimeException); - LOGGER.error(em.getId(), runtimeException); - return new RestResponse<>(returnCode, returnMessage, em); + public RestResponse runtimeException(RuntimeException exception) { + return new RestResponse<>(ErrorCode.ERROR, buildError(exception)); } /** - * 描述 : 构造错误信息(部分) + * 描述 : 构造错误响应对象 * - * @param httpStatus http状态 - * @param errorCode 错误代码 - * @param errorMessage 错误描述 - * @param outputExceptionStackTrace 是否输出异常堆栈 * @param exception 异常 - * @return 错误信息 + * @return 错误响应对象 */ - public static ErrorMessage buildErrorMessage(int httpStatus, String errorCode, - String errorMessage, boolean outputExceptionStackTrace, Exception exception) { - ErrorMessage em = new ErrorMessage(); - em.setId(UUID.randomUUID().toString()); - em.setHttpStatus(httpStatus); - em.setErrorCode(errorCode); - em.setErrorMsg(errorMessage); - em.setExceptionType(exception.getClass().getName()); - em.setExceptionMessage(ExceptionUtils.getMessage(exception)); - em.setErrorDate(new Date()); - if (outputExceptionStackTrace) { - em.setExceptionStackTrace(ExceptionUtils.getStackTrace(exception)); + private ErrorResult buildError(Exception exception) { + ErrorResult error = new ErrorResult(); + error.setExceptionType(exception.getClass().getName()); + error.setExceptionMessage(ExceptionUtils.getMessage(exception)); + if (applicationConfig.isOutputExceptionStackTrace()) { + error.setExceptionStackTrace(ExceptionUtils.getStackTrace(exception)); } - return em; + error.setExceptionDate(new Date()); + LOGGER.error(exception.getClass().getName(), exception); + return error; } }