Browse Source

配置优化

yucheng 3 years ago
parent
commit
6045d800a3

+ 111 - 0
dc-common/src/main/java/com/persagy/dc/common/handler/CommonExceptionHandler.java

@@ -0,0 +1,111 @@
+package com.persagy.dc.common.handler;
+
+import com.persagy.dc.common.constant.ResponseCode;
+import com.persagy.dc.common.exception.BusinessException;
+import com.persagy.dc.common.model.response.CommonResult;
+import com.persagy.dc.common.utils.ResultHelper;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.converter.HttpMessageNotReadableException;
+import org.springframework.validation.BindException;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.servlet.NoHandlerFoundException;
+
+import javax.validation.ValidationException;
+
+/**
+ * @description: 通用异常处理类
+ * @author: lixing
+ * @since: 2021/3/5 5:10 下午
+ **/
+@RestControllerAdvice
+@Slf4j
+public class CommonExceptionHandler {
+
+    /**
+     * 空指针异常
+     */
+    @ExceptionHandler(NullPointerException.class)
+    public CommonResult handleBusinessRuntimeException(NullPointerException e) {
+        log.error(e.getMessage(), e);
+        return ResultHelper.failure(ResponseCode.B0001.getCode(), e.getMessage());
+    }
+
+    /**
+     * 自定义业务异常
+     */
+    @ExceptionHandler(BusinessException.class)
+    public CommonResult handleBusinessRuntimeException(BusinessException e) {
+        log.error(e.getMessage(), e);
+        return ResultHelper.failure(e.getErrorCode(), e.getErrorDesc());
+    }
+
+    /**
+     * 入参校验异常
+     */
+    @ExceptionHandler(MethodArgumentNotValidException.class)
+    public CommonResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
+        log.error(e.getMessage(), e);
+        return ResultHelper.failure(ResponseCode.A0400.getCode(), e.getBindingResult().getFieldError().getDefaultMessage());
+    }
+
+    /**
+     * 入参校验异常
+     */
+    @ExceptionHandler(BindException.class)
+    public CommonResult handleBindException(BindException e) {
+        log.error(e.getMessage(), e);
+        return ResultHelper.failure(ResponseCode.A0400.getCode(), e.getBindingResult().getFieldError().getDefaultMessage());
+    }
+
+    /**
+     * 参数校验异常,message 为手动抛出时定义的具体异常信息
+     */
+    @ExceptionHandler(ValidationException.class)
+    public CommonResult handleValidationException(ValidationException e) {
+        log.error(e.getMessage(), e);
+        return ResultHelper.failure(ResponseCode.A0400.getCode(), e.getCause().getMessage());
+    }
+
+    /**
+     * 参数校验异常,message 为手动抛出时定义的具体异常信息
+     */
+    @ExceptionHandler(IllegalArgumentException.class)
+    public CommonResult handleIllegalArgumentException(IllegalArgumentException e) {
+        log.error(e.getMessage(), e);
+        return ResultHelper.failure(ResponseCode.A0400.getCode(), e.getMessage());
+    }
+
+    /**
+     * URI请求链接没有映射
+     */
+    @ResponseStatus(HttpStatus.NOT_FOUND)
+    @ExceptionHandler(NoHandlerFoundException.class)
+    public CommonResult handleNoHandlerFoundException(NoHandlerFoundException e) {
+        log.error(e.getMessage(), e);
+        return ResultHelper.failure(ResponseCode.A0422.getCode(), e.getMessage());
+    }
+
+    /**
+     *  body体未找到
+     */
+    @ResponseStatus(HttpStatus.BAD_REQUEST)
+    @ExceptionHandler(HttpMessageNotReadableException.class)
+    public CommonResult handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
+        log.error(e.getMessage(), e);
+        return ResultHelper.failure(ResponseCode.A0402.getCode(), ResponseCode.A0402.getDesc());
+    }
+
+    /**
+     * 系统默认繁忙
+     */
+    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
+    @ExceptionHandler(Exception.class)
+    public CommonResult handleException(Exception e) {
+        log.error(e.getMessage(), e);
+        return ResultHelper.failure(ResponseCode.Z9999.getCode(), ResponseCode.Z9999.getDesc());
+    }
+}

+ 3 - 6
dc-comp/dc-mybatis/README.md

@@ -8,6 +8,7 @@ mybatis-plus封装
 说明
 ---------------
 多数据源
+* 启用多数据源(默认为不启用):persagy.common.dynamic.enabled=true
 * 配置文件参考:com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties
 * [配置文件示例](https://github.com/dynamic-datasource/dynamic-datasource-doc/blob/master/docs/guide/integration/Druid.md)
 ```
@@ -164,12 +165,8 @@ spring:
 树型内部码工具
 * 可对实现ITreeEntity的model自动生成innerCode内部码。
 	
-自定义日期类型转换
-* 全局处理日期类型转换char,需要增加配置
-  ```
-  mybatis-plus:
-    typeHandlersPackage: com.persagy.**.typehandler
-  ```
+自定义DB数据格式转换器
+* 放入此目录中即可: com.persagy.**.typehandler
 
 最新变化
 ---------------

+ 5 - 3
dc-comp/dc-mybatis/src/main/java/com/persagy/dc/mybatis/handler/CommonMetaObjectHandler.java

@@ -2,6 +2,7 @@ package com.persagy.dc.mybatis.handler;
 
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 import com.persagy.dc.common.constant.ValidEnum;
+import com.persagy.dc.common.lang.PsDateTime;
 import com.persagy.dc.common.model.entity.AuditableEntity;
 import com.persagy.dc.common.model.entity.BaseEntity;
 import org.apache.ibatis.reflection.MetaObject;
@@ -18,13 +19,14 @@ import java.util.Date;
  **/
 @Component
 public class CommonMetaObjectHandler implements MetaObjectHandler {
+
     @Override
     public void insertFill(MetaObject metaObject) {
         if (metaObject.hasSetter(AuditableEntity.DO_PROP_CREATIONTIME)) {
-            this.setFieldValByName(AuditableEntity.DO_PROP_CREATIONTIME, new Date(), metaObject);
+            this.setFieldValByName(AuditableEntity.DO_PROP_CREATIONTIME, new PsDateTime(), metaObject);
         }
         if (metaObject.hasSetter(AuditableEntity.DO_PROP_MODIFIEDTIME)) {
-            this.setFieldValByName(AuditableEntity.DO_PROP_MODIFIEDTIME, new Date(), metaObject);
+            this.setFieldValByName(AuditableEntity.DO_PROP_MODIFIEDTIME, new PsDateTime(), metaObject);
         }
         if (metaObject.hasSetter(BaseEntity.PROP_VALID)) {
             this.setFieldValByName(BaseEntity.PROP_VALID, ValidEnum.TRUE.getType(), metaObject);
@@ -34,7 +36,7 @@ public class CommonMetaObjectHandler implements MetaObjectHandler {
     @Override
     public void updateFill(MetaObject metaObject) {
         if (metaObject.hasSetter(AuditableEntity.DO_PROP_MODIFIEDTIME)) {
-            this.setFieldValByName(AuditableEntity.DO_PROP_MODIFIEDTIME, new Date(), metaObject);
+            this.setFieldValByName(AuditableEntity.DO_PROP_MODIFIEDTIME, new PsDateTime(), metaObject);
         }
     }
 }

+ 1 - 1
dc-comp/dc-mybatis/src/main/java/com/persagy/dc/mybatis/handler/DynamicDataSourceHandler.java

@@ -36,7 +36,7 @@ public class DynamicDataSourceHandler extends HandlerInterceptorAdapter {
     private DynamicDataSourceProperties defaultProperty;
     @Autowired
     private IDbService dbService;
-    @Value("${spring.datasource.dynamic.enabled:false}")
+    @Value("${persagy.common.dynamic.enabled:false}")
     private boolean dynamicEnabled;
 
     @Override