build: 参数校验返回result holder

This commit is contained in:
CaptainB 2023-05-15 15:02:08 +08:00 committed by 刘瑞斌
parent 58d8054d67
commit 06fcf73716
2 changed files with 12 additions and 33 deletions

View File

@ -17,13 +17,14 @@ public class RestControllerExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, String> handleValidationExceptions(MethodArgumentNotValidException ex) {
public ResultHolder handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> 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);
}
}

View File

@ -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);
}