From 06fcf737162c6629c992098c5295b01dd1f48e82 Mon Sep 17 00:00:00 2001 From: CaptainB Date: Mon, 15 May 2023 15:02:08 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E5=8F=82=E6=95=B0=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E8=BF=94=E5=9B=9Eresult=20holder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RestControllerExceptionHandler.java | 5 ++- .../sdk/controller/handler/ResultHolder.java | 40 +++++-------------- 2 files changed, 12 insertions(+), 33 deletions(-) diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/RestControllerExceptionHandler.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/RestControllerExceptionHandler.java index f5a849e50b..e9173b97c1 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/RestControllerExceptionHandler.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/RestControllerExceptionHandler.java @@ -17,13 +17,14 @@ public class RestControllerExceptionHandler { @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MethodArgumentNotValidException.class) - public Map handleValidationExceptions(MethodArgumentNotValidException ex) { + public ResultHolder handleValidationExceptions(MethodArgumentNotValidException ex) { Map errors = new HashMap<>(); ex.getBindingResult().getAllErrors().forEach((error) -> { String fieldName = ((FieldError) error).getField(); String errorMessage = error.getDefaultMessage(); errors.put(fieldName, errorMessage); }); - return errors; + // 错误码待定 + return ResultHolder.error(40000, "Validation failed", errors); } } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/ResultHolder.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/ResultHolder.java index a282ba4ff2..a8163e1940 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/ResultHolder.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/ResultHolder.java @@ -1,9 +1,18 @@ package io.metersphere.sdk.controller.handler; +import lombok.Data; import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; +@Data public class ResultHolder { + // 请求是否成功 + private int code = 0; + // 描述信息 + private String message; + // 返回数据 + private Object data = ""; + public ResultHolder() { this.code = 0; } @@ -24,37 +33,6 @@ public class ResultHolder { this.data = data; } - // 请求是否成功 - private int code = 0; - // 描述信息 - private String message; - // 返回数据 - private Object data = ""; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - public static ResultHolder success(Object obj) { return new ResultHolder(obj); }