ソースを参照

通用注解校验数据

gaodm 6 年 前
コミット
dc49d0421c

+ 4 - 3
common/src/main/java/com/diagbot/exception/CommonErrorCode.java

@@ -9,9 +9,10 @@ package com.diagbot.exception;
  */
 public enum CommonErrorCode implements ErrorCode {
 
-    OK("0", "操作成功"),
-    FAIL("-1", "操作失败"),
-    RPC_ERROR("-2","远程调度失败"),
+    OK("00000000", "操作成功"),
+    FAIL("00000001", "操作失败"),
+    RPC_ERROR("00000002","远程调度失败"),
+    PARAM_ERROR("00029999", "%s"), //参数错误
     NOT_EXISTS ("00020001", "该数据不存在!"),
     INSERT_DATA_FAILED("00020002", "数据库写入失败!"),
     UPDATE_INFO_FAIL("00020003", "更新数据失败!"),

+ 39 - 1
user-service/src/main/java/com/diagbot/exception/CommonExceptionHandler.java

@@ -1,13 +1,21 @@
 package com.diagbot.exception;
 
 import com.diagbot.dto.RespDTO;
+import com.diagbot.util.GsonUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
+import org.springframework.validation.BindException;
+import org.springframework.validation.FieldError;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+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 java.util.HashMap;
+import java.util.Map;
+
 
 /**
  * @Description: 错误通用处理
@@ -22,6 +30,36 @@ public class CommonExceptionHandler {
     @ExceptionHandler(Exception.class)
     public ResponseEntity<RespDTO> handleException(Exception e) {
         RespDTO resp = new RespDTO();
+        if(e instanceof BindException) {
+            BindException ex = (BindException) e;
+            Map<String, String> stringMap = new HashMap<>();
+            for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
+                stringMap.put(fieldError.getField(), fieldError.getDefaultMessage());
+            }
+            log.warn("【参数异常】:", ex.getBindingResult());
+            resp.code = CommonErrorCode.PARAM_ERROR.getCode();
+            resp.msg = GsonUtil.toJson(stringMap);
+            return new ResponseEntity(resp, HttpStatus.OK);
+        }
+        if(e instanceof MethodArgumentNotValidException) {
+            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
+            Map<String, String> stringMap = new HashMap<>();
+            for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
+                stringMap.put(fieldError.getField(), fieldError.getDefaultMessage());
+            }
+            log.warn("【参数异常】:", ex.getBindingResult());
+            resp.code = CommonErrorCode.PARAM_ERROR.getCode();
+            resp.msg = GsonUtil.toJson(stringMap);
+            return new ResponseEntity(resp, HttpStatus.OK);
+        }
+        if(e instanceof MissingServletRequestParameterException) {
+            MissingServletRequestParameterException ex = (MissingServletRequestParameterException) e;
+            Map<String, String> stringMap = new HashMap<>();
+            stringMap.put(ex.getParameterName(), "不能为null");
+            resp.code = CommonErrorCode.PARAM_ERROR.getCode();
+            resp.msg = GsonUtil.toJson(stringMap);
+            return new ResponseEntity(resp, HttpStatus.OK);
+        }
         if(e instanceof CommonException) {
             CommonException taiChiException = (CommonException) e;
             resp.code = taiChiException.getCode();
@@ -29,7 +67,7 @@ public class CommonExceptionHandler {
             log.error("【业务异常】:" + e.getMessage());
             return new ResponseEntity(resp, HttpStatus.OK);
         }
-        resp.code = "-1";
+        resp.code = CommonErrorCode.FAIL.getCode();
         resp.msg = e.getMessage();
         log.error("【系统异常】:" + e.getMessage());
         e.printStackTrace();

+ 4 - 0
user-service/src/main/java/com/diagbot/vo/ImgVerVerVO.java

@@ -3,6 +3,8 @@ package com.diagbot.vo;
 import lombok.Getter;
 import lombok.Setter;
 
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
 import java.util.Date;
 
 /**
@@ -14,7 +16,9 @@ import java.util.Date;
 @Setter
 public class ImgVerVerVO {
     //图片验证码唯一标志
+    @NotBlank(message = "图片验证码唯一标志不能为空!")
     private String imgId;
     //验证码
+    @NotBlank(message = "验证码不能为空!")
     private String code;
 }

+ 3 - 1
user-service/src/main/java/com/diagbot/web/VerController.java

@@ -14,6 +14,8 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.validation.Valid;
+
 /**
  * @Description: 用户验证API
  * @author: gaodm
@@ -44,7 +46,7 @@ public class VerController {
     @ApiOperation(value = "验证图片验证码", notes = "图片验证码唯一标志后和验证码不能为空")
     @PostMapping("/verifyImgVerification")
     @SysLogger("verifyImgVerification")
-    public RespDTO verImgVerification(@RequestBody ImgVerVerVO imgVerVerVO){
+    public RespDTO verImgVerification(@RequestBody @Valid ImgVerVerVO imgVerVerVO){
         return   RespDTO.onSuc(verFacade.verifyImgVerification(imgVerVerVO));
     }