From b8f1e63cbde5e6a756ad760339f0719273759a54 Mon Sep 17 00:00:00 2001 From: Administrator Date: Thu, 22 Oct 2020 14:09:50 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=A2=9E=E5=8A=A0=E4=BA=86libs=E5=AD=90?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=EF=BC=8C=20=E6=94=BE=E7=BD=AE=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E9=A1=B9=E7=9B=AE=202.=20MVC=20Starter=20=E4=B8=AD?= =?UTF-8?q?=E5=A2=9E=EF=BC=9A=20=20=20=20=201=EF=BC=89=20=E5=8A=A0?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=20=20=20?= =?UTF-8?q?=20=202=EF=BC=89=20=E7=BB=9F=E4=B8=80=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=EF=BC=9A=20BaseResponse=20=20=20=20=203=EF=BC=89=20=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=E5=BC=82=E5=B8=B8=E7=B1=BB=EF=BC=9A=20BusinessExcepti?= =?UTF-8?q?on=20=20=20=20=204=EF=BC=89=20=E9=80=9A=E7=94=A8=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E6=9E=9A=E4=B8=BE:=20ResponseEnum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mall-ebtp-cloud-common/pom.xml | 66 +++++ .../mall/cloud/common/utils/JsonUtils.java | 97 +++++++ .../ebtp/mall/cloud/common/AppTest.java | 38 +++ mall-ebtp-cloud-mvc-starter/pom.xml | 18 +- .../BusinessExceptionHandlerAdvice.java | 254 ++++++++++++++++++ .../cloud/mvc/starter/base/BaseResponse.java | 65 +++++ .../cloud/mvc/starter/enums/ResponseEnum.java | 48 ++++ .../cloud/mvc/starter/exception/Assert.java | 75 ++++++ .../starter/exception/BusinessException.java | 36 +++ .../exception/BusinessExceptionAssert.java | 33 +++ .../mvc/starter/exception/IResponseEnum.java | 19 ++ mall-ebtp-cloud-parent/pom.xml | 5 +- pom.xml | 5 + 13 files changed, 755 insertions(+), 4 deletions(-) create mode 100644 mall-ebtp-cloud-libs/mall-ebtp-cloud-common/mall-ebtp-cloud-common/pom.xml create mode 100644 mall-ebtp-cloud-libs/mall-ebtp-cloud-common/mall-ebtp-cloud-common/src/main/java/com/chinaunicom/ebtp/mall/cloud/common/utils/JsonUtils.java create mode 100644 mall-ebtp-cloud-libs/mall-ebtp-cloud-common/mall-ebtp-cloud-common/src/test/java/om/chinaunicom/ebtp/mall/cloud/common/AppTest.java create mode 100644 mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/advice/BusinessExceptionHandlerAdvice.java create mode 100644 mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/base/BaseResponse.java create mode 100644 mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/enums/ResponseEnum.java create mode 100644 mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/Assert.java create mode 100644 mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/BusinessException.java create mode 100644 mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/BusinessExceptionAssert.java create mode 100644 mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/IResponseEnum.java diff --git a/mall-ebtp-cloud-libs/mall-ebtp-cloud-common/mall-ebtp-cloud-common/pom.xml b/mall-ebtp-cloud-libs/mall-ebtp-cloud-common/mall-ebtp-cloud-common/pom.xml new file mode 100644 index 0000000..9091e46 --- /dev/null +++ b/mall-ebtp-cloud-libs/mall-ebtp-cloud-common/mall-ebtp-cloud-common/pom.xml @@ -0,0 +1,66 @@ + + 4.0.0 + + com.chinaunicom.ebtp + mall-ebtp-cloud-common + 0.0.1 + jar + + mall-ebtp-cloud-common + + + 1.8 + @ + ${java.version} + ${java.version} + UTF-8 + UTF-8 + + + + + cn.hutool + hutool-all + 5.4.1 + + + com.fasterxml.jackson.module + jackson-module-parameter-names + 2.12.0-rc1 + + + com.fasterxml.jackson.datatype + jackson-datatype-jdk8 + 2.12.0-rc1 + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + 2.12.0-rc1 + + + org.projectlombok + lombok + 1.18.12 + + + org.slf4j + slf4j-api + 1.7.25 + + + org.slf4j + slf4j-log4j12 + 1.7.25 + + + junit + junit + 3.8.1 + test + + + + diff --git a/mall-ebtp-cloud-libs/mall-ebtp-cloud-common/mall-ebtp-cloud-common/src/main/java/com/chinaunicom/ebtp/mall/cloud/common/utils/JsonUtils.java b/mall-ebtp-cloud-libs/mall-ebtp-cloud-common/mall-ebtp-cloud-common/src/main/java/com/chinaunicom/ebtp/mall/cloud/common/utils/JsonUtils.java new file mode 100644 index 0000000..218be88 --- /dev/null +++ b/mall-ebtp-cloud-libs/mall-ebtp-cloud-common/mall-ebtp-cloud-common/src/main/java/com/chinaunicom/ebtp/mall/cloud/common/utils/JsonUtils.java @@ -0,0 +1,97 @@ +package com.chinaunicom.ebtp.mall.cloud.common.utils; + +import java.util.List; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; + +import cn.hutool.core.exceptions.ExceptionUtil; +import com.fasterxml.jackson.databind.JavaType; +import lombok.extern.slf4j.Slf4j; + +/** + * json util + * + * @author 付庆吉 + * @date 2020-09-16 + */ +@Slf4j +public class JsonUtils { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + /** + * 将对象转换成json字符串。 + *

Title: pojoToJson

+ *

Description:

+ * + * @param data + * @return + */ + public static String objectToJson(Object data) { + try { + MAPPER.registerModule(new ParameterNamesModule()) + .registerModule(new Jdk8Module()) + .registerModule(new JavaTimeModule()); + return MAPPER.writeValueAsString(data); + } catch (JsonProcessingException e) { + log.info(ExceptionUtil.stacktraceToString(e)); + } + return null; + } + + /** + * 将json结果集转化为对象 + * + * @param jsonData json数据 + * @param beanType 对象中的object类型 + * @return + */ + public static T jsonToPojo(String jsonData, Class beanType) { + try { + return MAPPER.readValue(jsonData, beanType); + } catch (Exception e) { + log.info(ExceptionUtil.stacktraceToString(e)); + } + return null; + } + + /** + * 将json数据转换成pojo对象list + *

Title: jsonToList

+ *

Description:

+ * + * @param jsonData + * @param beanType + * @return + */ + public static List jsonToList(String jsonData, Class beanType) { + JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); + try { + List list = MAPPER.readValue(jsonData, javaType); + return list; + } catch (Exception e) { + log.info(ExceptionUtil.stacktraceToString(e)); + } + return null; + } + + /** + * 将List转为List + *

Title: jsonToList

+ *

Description:

+ * + * @param jsonData + * @param beanType + * @return + */ + public static List jsonToList(List jsonData, Class beanType) { + String str = objectToJson(jsonData); + + return jsonToList(str,beanType); + } + +} diff --git a/mall-ebtp-cloud-libs/mall-ebtp-cloud-common/mall-ebtp-cloud-common/src/test/java/om/chinaunicom/ebtp/mall/cloud/common/AppTest.java b/mall-ebtp-cloud-libs/mall-ebtp-cloud-common/mall-ebtp-cloud-common/src/test/java/om/chinaunicom/ebtp/mall/cloud/common/AppTest.java new file mode 100644 index 0000000..a9557e8 --- /dev/null +++ b/mall-ebtp-cloud-libs/mall-ebtp-cloud-common/mall-ebtp-cloud-common/src/test/java/om/chinaunicom/ebtp/mall/cloud/common/AppTest.java @@ -0,0 +1,38 @@ +package om.chinaunicom.ebtp.mall.cloud.common; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/mall-ebtp-cloud-mvc-starter/pom.xml b/mall-ebtp-cloud-mvc-starter/pom.xml index 3a647ea..c710755 100644 --- a/mall-ebtp-cloud-mvc-starter/pom.xml +++ b/mall-ebtp-cloud-mvc-starter/pom.xml @@ -14,11 +14,11 @@ mall-ebtp-cloud-mvc-starter 0.0.1 mall-ebtp-cloud-mvc-starter - + Chinaunicom, Inc. - + Ajaxfan @@ -26,11 +26,23 @@ Chinaunicom, Inc. - + org.springframework.boot spring-boot-starter-web + + cn.hutool + hutool-all + + + com.chinaunicom.ebtp + mall-ebtp-cloud-common + + + org.springframework + spring-tx + diff --git a/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/advice/BusinessExceptionHandlerAdvice.java b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/advice/BusinessExceptionHandlerAdvice.java new file mode 100644 index 0000000..118b8e0 --- /dev/null +++ b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/advice/BusinessExceptionHandlerAdvice.java @@ -0,0 +1,254 @@ +package com.chinaunicom.mall.ebtp.cloud.mvc.starter.advice; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.http.HttpStatus; +import org.springframework.validation.BindException; +import org.springframework.validation.FieldError; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.MissingServletRequestParameterException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; +import org.yaml.snakeyaml.constructor.DuplicateKeyException; + +import com.chinaunicom.ebtp.mall.cloud.common.utils.JsonUtils; +import com.chinaunicom.mall.ebtp.cloud.mvc.starter.base.BaseResponse; +import com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception.BusinessException; + +import cn.hutool.core.convert.Convert; +import cn.hutool.core.exceptions.ExceptionUtil; +import lombok.Getter; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; + +/** + * 异常处理 拦截BindException异常,返回HttpStatus是400的绑定错误信息 + * 拦截FrameException异常,返回HttpStatus是406的业务处理错误信息(支持自定义状态码) + * 拦截Exception异常,返回HttpStatus是500服务器内部异常 + * + * @file: com.chinaunicom.mall.ebtp.cloud.mvc.starter.advice.BusinessExceptionHandlerAdvice + * @description: + * @author fqj + * @date 2020年9月3日 + * @version: V1.0 + * @update + */ +@Slf4j +@ControllerAdvice +@ResponseBody +@ConditionalOnProperty(name = "mconfig.exception-handle-enabled", matchIfMissing = true) +public class BusinessExceptionHandlerAdvice { + + /** + * 业务异常处理 + * + * @param request 请求 + * @param exception ServiceErrorException异常对象 + * @return 响应 + */ + @ExceptionHandler(value = BusinessException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public BaseResponse serviceErrorException(HttpServletRequest request, BusinessException exception) { + log.error(ExceptionUtil.stacktraceToString(exception)); + + Map body = new HashMap<>(); + body.put("path", request.getRequestURI()); + + return BaseResponse.fail(exception.getCode(), exception.getMessage(), Convert.toStr(body)); + } + + /** + * 参数绑定异常 + * + * @param request 请求 + * @param exception BindException异常对象 + * @return 响应 + */ + @ExceptionHandler(value = BindException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public BaseResponse methodArgumentNotValidHandler(HttpServletRequest request, BindException exception) { + // 按需重新封装需要返回的错误信息 + List invalidArguments = new ArrayList<>(); + + // 解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息 + for (FieldError error : exception.getBindingResult().getFieldErrors()) { + ArgumentInvalidResult invalidArgument = new ArgumentInvalidResult(); + invalidArgument.setDefaultMessage(error.getDefaultMessage()); + invalidArgument.setField(error.getField()); + invalidArgument.setRejectedValue(error.getRejectedValue()); + invalidArguments.add(invalidArgument); + } + Map body = new HashMap<>(); + body.put("errors", JsonUtils.objectToJson(invalidArguments)); + body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase()); + body.put("path", request.getRequestURI()); + + return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "参数错误", Convert.toStr(body)); + } + + /** + * 请求方式异常 + * + * @param request 请求 + * @param exception BindException异常对象 + * @return 响应 + */ + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) + public BaseResponse handleMethodNotSupportedException(HttpServletRequest request, + HttpRequestMethodNotSupportedException exception) { + Map body = new HashMap<>(); + body.put("errors", exception.getMessage()); + body.put("error", HttpStatus.METHOD_NOT_ALLOWED.getReasonPhrase()); + body.put("path", request.getRequestURI()); + + return BaseResponse.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), "错误的请求方式", Convert.toStr(body)); + } + + /** + * 参数缺失 + * + * @param request 请求 + * @param exception BindException异常对象 + * @return 响应 + */ + @ExceptionHandler(MissingServletRequestParameterException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public BaseResponse handleMissingParameterException(HttpServletRequest request, + MissingServletRequestParameterException exception) { + Map body = new HashMap<>(); + body.put("errors", exception.getMessage()); + body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase()); + body.put("path", request.getRequestURI()); + + return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "参数缺失", Convert.toStr(body)); + } + + // ---------------------------- 数据库操作相关异常 -------------------------------------- + + /** + * 数据库异常 + * + * @param request 请求 + * @param exception BindException异常对象 + * @return 响应 + */ + @ExceptionHandler(DataAccessException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public BaseResponse handlerDataAccessException(HttpServletRequest request, DataAccessException exception) { + log.error(ExceptionUtil.stacktraceToString(exception)); + Map body = new HashMap<>(); + body.put("errors", exception.getMessage()); + body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase()); + body.put("path", request.getRequestURI()); + + return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "数据库异常", Convert.toStr(body)); + } + + /** + * 数据不存在 + * + * @param request 请求 + * @param exception BindException异常对象 + * @return 响应 + */ + @ExceptionHandler(EmptyResultDataAccessException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public BaseResponse handleDataEmptyException(HttpServletRequest request, + EmptyResultDataAccessException exception) { + Map body = new HashMap<>(); + body.put("errors", exception.getMessage()); + body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase()); + body.put("path", request.getRequestURI()); + + return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "数据不存在", Convert.toStr(body)); + } + + /** + * 请求方式异常 + * + * @param request 请求 + * @param exception BindException异常对象 + * @return 响应 + */ + @ExceptionHandler(DuplicateKeyException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public BaseResponse handleDataDualException(HttpServletRequest request, DuplicateKeyException exception) { + Map body = new HashMap<>(); + body.put("errors", exception.getMessage()); + body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase()); + body.put("path", request.getRequestURI()); + + return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "数据重复插入", Convert.toStr(body)); + } + + /** + * 方法参数类型不匹配异常 + * + * @param request 请求 + * @param exception BindException异常对象 + * @return 响应 + */ + @ExceptionHandler(MethodArgumentTypeMismatchException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public BaseResponse handleMethodArgumentTypeException(HttpServletRequest request, + MethodArgumentTypeMismatchException exception) { + Map body = new HashMap<>(); + body.put("errors", exception.getMessage()); + body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase()); + body.put("path", request.getRequestURI()); + + return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "参数类型不匹配", Convert.toStr(body)); + } + + /** + * 全局异常处理 + * + * @param request 请求 + * @param exception Exception异常对象 + * @return 响应 MethodArgumentNotValidException + */ + @ExceptionHandler(value = Exception.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public BaseResponse handleException(HttpServletRequest request, Exception exception) { + log.error(ExceptionUtil.stacktraceToString(exception)); + Map body = new HashMap<>(); + body.put("errors", exception.getMessage()); + body.put("error", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()); + body.put("path", request.getRequestURI()); + + return BaseResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "网络异常", Convert.toStr(body)); + } + + /** + * 参数异常 + */ + @Getter + @Setter + class ArgumentInvalidResult { + /** + * 字段名 + */ + private String field; + /** + * 输入的错误值 + */ + private Object rejectedValue; + /** + * 错误信息 + */ + private String defaultMessage; + } + +} diff --git a/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/base/BaseResponse.java b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/base/BaseResponse.java new file mode 100644 index 0000000..60739a6 --- /dev/null +++ b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/base/BaseResponse.java @@ -0,0 +1,65 @@ +package com.chinaunicom.mall.ebtp.cloud.mvc.starter.base; + +import java.io.Serializable; + +import com.chinaunicom.mall.ebtp.cloud.mvc.starter.enums.ResponseEnum; +import com.fasterxml.jackson.annotation.JsonInclude; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * 返回类型FrameResponse + */ +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class BaseResponse implements Serializable { + + private static final long serialVersionUID = 6769157532172136264L; + + private Integer code; + + private String message; + + @JsonInclude(JsonInclude.Include.NON_NULL) + private T data; + + public static BaseResponse success() { + return new BaseResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage(), ""); + } + + public static BaseResponse success(T data) { + return new BaseResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage(), data); + } + + public static BaseResponse success(String message, T data) { + return new BaseResponse<>(ResponseEnum.SUCCESS.getCode(), message, data); + } + + public static BaseResponse fail() { + return new BaseResponse<>(ResponseEnum.ERROR.getCode(), ResponseEnum.ERROR.getMessage(), ""); + } + + public static BaseResponse fail(String message) { + return new BaseResponse<>(ResponseEnum.ERROR.getCode(), message, ""); + } + + public static BaseResponse fail(Integer code, String message) { + return new BaseResponse<>(code, message, ""); + } + + public static BaseResponse fail(T data) { + return new BaseResponse<>(ResponseEnum.ERROR.getCode(), ResponseEnum.ERROR.getMessage(), data); + } + + public static BaseResponse fail(String message, T data) { + return new BaseResponse<>(ResponseEnum.ERROR.getCode(), message, data); + } + + public static BaseResponse fail(Integer code, String message, T data) { + return new BaseResponse(code, message, data); + } + +} \ No newline at end of file diff --git a/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/enums/ResponseEnum.java b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/enums/ResponseEnum.java new file mode 100644 index 0000000..5654b27 --- /dev/null +++ b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/enums/ResponseEnum.java @@ -0,0 +1,48 @@ +package com.chinaunicom.mall.ebtp.cloud.mvc.starter.enums; + +import com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception.BusinessExceptionAssert; + +import lombok.AllArgsConstructor; +import lombok.Getter; + + +/** + * 通用异常Enum + * + * @author 付庆吉 + * @date 2020-09-14 + */ +@Getter +@AllArgsConstructor +public enum ResponseEnum implements BusinessExceptionAssert { + + /** + * 成功 + */ + SUCCESS(0, "success"), + + /** + * 失败 + */ + ERROR(90000, "操作失败"), + + /** + * token not find + */ + TOKEN_NOT_FIND(30001, "认证不存在!"), + /** + * connect timeout + */ + CONNECT_TIMEOUT(30002, "连接超时!"); + + /** + * 返回码 + */ + private int code; + /** + * 返回消息 + */ + private String message; + + +} \ No newline at end of file diff --git a/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/Assert.java b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/Assert.java new file mode 100644 index 0000000..3260779 --- /dev/null +++ b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/Assert.java @@ -0,0 +1,75 @@ +package com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception; + +/** + * 通用断言接口 + * + * @file: com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception.Assert + * @description: + * @author 付庆吉 + * @date 2020-09-14 + * @version: V1.0 + * @update + */ +public interface Assert { + + /** + * 创建异常 + * + * @param args + * @return + */ + BusinessException newException(Object... args); + + /** + * 创建异常 + * + * @param t + * @param args + * @return + */ + BusinessException newException(Throwable t, Object... args); + + /** + *

断言对象obj非空。如果对象obj为空,则抛出异常 + * + * @param obj 待判断对象 + */ + default void assertNotNull(Object obj) { + if (obj == null) { + throw newException(obj); + } + } + + /** + *

断言对象args非空。如果对象args为null或空字符串,则抛出异常 + * + * @param args 待判断字符串 + */ + default void assertStrNotNull(String... args) { + this.assertNotNull(args); + for (String arg : args) { + if (arg == null || arg.isEmpty()) { + throw newException(arg); + } + } + } + + /** + * 根据传参抛出异常,则抛出异常 + * + * @param t 验证结果 + */ + default void customValid(boolean t) { + if (t) { + throw newException(); + } + } + + /** + * 抛出异常 + */ + default void throwException() { + throw newException(); + } + +} \ No newline at end of file diff --git a/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/BusinessException.java b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/BusinessException.java new file mode 100644 index 0000000..cb1910b --- /dev/null +++ b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/BusinessException.java @@ -0,0 +1,36 @@ +package com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception; + +import lombok.Getter; +import lombok.Setter; + +/** + * 平台自定义异常类 + * + * @file: com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception.BusinessException + * @description: 平台自定义异常类 + * @author: ajaxfan + * @date: 2020-10-22 + * @version: V1.0 + * @update + */ +@Getter +@Setter +public class BusinessException extends RuntimeException { + private static final long serialVersionUID = 1L; + + private Integer code; + private String message; + + public BusinessException(IResponseEnum iResponseEnum, String message) { + super(message); + this.code = iResponseEnum.getCode(); + this.message = message; + } + + public BusinessException(IResponseEnum iResponseEnum, String message, Throwable cause) { + super(message, cause); + this.code = iResponseEnum.getCode(); + this.message = message; + } + +} diff --git a/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/BusinessExceptionAssert.java b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/BusinessExceptionAssert.java new file mode 100644 index 0000000..25dcf38 --- /dev/null +++ b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/BusinessExceptionAssert.java @@ -0,0 +1,33 @@ +package com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception; + +import java.text.MessageFormat; + +/** + * 初始化异常及断言接口,所有自定义异常Enum均需实现此接口 + *

+ * 自定义异常枚举中年必须有:code、message + * + * @file: com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception.BusinessExceptionAssert + * @description: + * @author 付庆吉 + * @date 2020-09-14 + * @version: V1.0 + * @update + */ +public interface BusinessExceptionAssert extends IResponseEnum, Assert { + + @Override + default BusinessException newException(Object... args) { + String msg = MessageFormat.format(this.getMessage(), args); + + return new BusinessException(this, msg); + } + + @Override + default BusinessException newException(Throwable t, Object... args) { + String msg = MessageFormat.format(this.getMessage(), args); + + return new BusinessException(this, msg, t); + } + +} \ No newline at end of file diff --git a/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/IResponseEnum.java b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/IResponseEnum.java new file mode 100644 index 0000000..9d5af26 --- /dev/null +++ b/mall-ebtp-cloud-mvc-starter/src/main/java/com/chinaunicom/mall/ebtp/cloud/mvc/starter/exception/IResponseEnum.java @@ -0,0 +1,19 @@ +package com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception; + +/** + * 异常编码和消息 + * + * @file: com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception.IResponseEnum + * @description: + * @author 付庆吉 + * @date 2020-09-14 + * @version: V1.0 + * @update + */ +public interface IResponseEnum { + + int getCode(); + + String getMessage(); + +} diff --git a/mall-ebtp-cloud-parent/pom.xml b/mall-ebtp-cloud-parent/pom.xml index 9173c79..3fd3266 100644 --- a/mall-ebtp-cloud-parent/pom.xml +++ b/mall-ebtp-cloud-parent/pom.xml @@ -337,7 +337,10 @@ - + + com.chinaunicom.ebtp + mall-ebtp-cloud-common + diff --git a/pom.xml b/pom.xml index d9efd18..522b6dd 100644 --- a/pom.xml +++ b/pom.xml @@ -97,6 +97,11 @@ mall-ebtp-cloud-feign-starter 0.0.1 + + com.chinaunicom.ebtp + mall-ebtp-cloud-common + 0.0.1 +