参数校验

This commit is contained in:
chen-xin-zhi 2025-04-29 11:49:04 +08:00
parent f36e4a38a8
commit e30312c743
5 changed files with 57 additions and 23 deletions

View File

@ -5,12 +5,19 @@ import com.greenorange.promotion.common.ErrorCode;
import com.greenorange.promotion.common.ResultUtils;
import io.swagger.v3.oas.annotations.Hidden;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.util.validation.ValidationError;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* 全局异常处理器
*/
@ -23,10 +30,19 @@ public class GlobalExceptionHandler {
// 处理参数绑定失败的异常
@ExceptionHandler(MethodArgumentNotValidException.class)
public BaseResponse<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
log.error("MethodArgumentNotValidException", e);
return ResultUtils.error(ErrorCode.PARAMS_ERROR, e.getMessage());
StringBuilder errors = new StringBuilder();
// 按字段名排序确保每次返回的顺序一致
e.getBindingResult().getFieldErrors().stream()
.sorted(Comparator.comparing(FieldError::getField)) // 按字段名排序
.forEach(fieldError -> errors.append("Field: ")
.append(fieldError.getField())
.append(" | Error: ")
.append(fieldError.getDefaultMessage())
.append("; "));
return ResultUtils.error(ErrorCode.PARAMS_ERROR, errors.toString());
}
// 处理消息体解析失败的异常
@ExceptionHandler(HttpMessageNotReadableException.class)
public BaseResponse<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {

View File

@ -2,7 +2,9 @@ package com.greenorange.promotion.model.dto.user;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@ -11,41 +13,41 @@ import java.io.Serializable;
*/
@Data
@Schema(description = "用户表添加请求体", requiredProperties = {"nickName", "userAvatar", "phoneNumber",
"userAccount", "userPassword", "invitationCode", "userRole", "parentUserId", "superUserList"})
"userAccount", "userPassword", "userRole"})
public class UserInfoAddRequest implements Serializable {
/**
* 用户昵称
*/
@NotBlank(message = "参数不能为空")
@NotBlank(message = "用户昵称不能为空")
@Schema(description = "用户昵称", example = "chenxinzhi")
private String nickName;
/**
* 用户头像URL
*/
@NotBlank(message = "参数不能为空")
@NotBlank(message = "用户头像URL不能为空")
@Schema(description = "用户头像URL", example = "http://xxx.png")
private String userAvatar;
/**
* 手机号
*/
@NotBlank(message = "参数不能为空")
@NotBlank(message = "手机号不能为空")
@Schema(description = "手机号", example = "15888610253")
private String phoneNumber;
/**
* 账号
*/
@NotBlank(message = "参数不能为空")
@NotBlank(message = "账号不能为空")
@Schema(description = "账号", example = "qingcheng_account")
private String userAccount;
/**
* 密码
*/
@NotBlank(message = "参数不能为空")
@NotBlank(message = "密码不能为空")
@Schema(description = "密码", example = "qingcheng_password")
private String userPassword;
@ -58,7 +60,7 @@ public class UserInfoAddRequest implements Serializable {
/**
* 用户角色
*/
@NotBlank(message = "参数不能为空")
@NotBlank(message = "用户角色不能为空")
@Schema(description = "用户角色", example = "user")
private String userRole;

View File

@ -1,6 +1,7 @@
package com.greenorange.promotion.model.dto.user;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import java.io.Serializable;
@ -15,12 +16,14 @@ public class UserInfoLoginRequest implements Serializable {
/**
* 账号
*/
@NotBlank(message = "账号不能为空")
@Schema(description = "账号", example = "${field.example}")
private String userAccount;
/**
* 密码
*/
@NotBlank(message = "密码不能为空")
@Schema(description = "密码", example = "${field.example}")
private String userPassword;

View File

@ -1,6 +1,8 @@
package com.greenorange.promotion.model.dto.user;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@ -14,14 +16,16 @@ import com.greenorange.promotion.common.PageRequest;
public class UserInfoQueryRequest extends PageRequest implements Serializable {
/**
* 用户 ID
* 用户ID
*/
@Schema(description = "用户表 ID", example = "1")
@NotNull(message = "用户ID不能为null")
@Schema(description = "用户ID", example = "1")
private Long id;
/**
* 手机号
*/
@NotBlank(message = "手机号不能为空")
@Schema(description = "手机号", example = "${field.example}")
private String phoneNumber;

View File

@ -1,6 +1,8 @@
package com.greenorange.promotion.model.dto.user;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.io.Serial;
@ -11,67 +13,74 @@ import java.io.Serializable;
*/
@Data
@Schema(description = "用户表更新请求体", requiredProperties = {"id", "nickName", "userAvatar", "phoneNumber",
"userAccount", "userPassword", "invitationCode", "userRole", "parentUserId", "superUserList"})
"userAccount", "userPassword", "userRole"})
public class UserInfoUpdateRequest implements Serializable {
/**
* 用户 ID
* 用户ID
*/
@Schema(description = "用户表 ID", example = "1")
@NotNull(message = "用户ID不能为null")
@Schema(description = "用户ID", example = "1")
private Long id;
/**
* 用户昵称
*/
@Schema(description = "用户昵称", example = "${field.example}")
@NotBlank(message = "用户昵称不能为空")
@Schema(description = "用户昵称", example = "chenxinzhi")
private String nickName;
/**
* 用户头像URL
*/
@Schema(description = "用户头像URL", example = "${field.example}")
@NotBlank(message = "用户头像URL不能为空")
@Schema(description = "用户头像URL", example = "http://xxx.png")
private String userAvatar;
/**
* 手机号
*/
@Schema(description = "手机号", example = "${field.example}")
@NotBlank(message = "手机号不能为空")
@Schema(description = "手机号", example = "15888610253")
private String phoneNumber;
/**
* 账号
*/
@Schema(description = "账号", example = "${field.example}")
@NotBlank(message = "账号不能为空")
@Schema(description = "账号", example = "qingcheng_account")
private String userAccount;
/**
* 密码
*/
@Schema(description = "密码", example = "${field.example}")
@NotBlank(message = "密码不能为空")
@Schema(description = "密码", example = "qingcheng_password")
private String userPassword;
/**
* 邀请码
*/
@Schema(description = "邀请码", example = "${field.example}")
@Schema(description = "邀请码", example = "666999")
private String invitationCode;
/**
* 用户角色
*/
@Schema(description = "用户角色", example = "${field.example}")
@NotBlank(message = "用户角色不能为空")
@Schema(description = "用户角色", example = "user")
private String userRole;
/**
* 上级用户id
*/
@Schema(description = "上级用户id", example = "${field.example}")
@Schema(description = "上级用户id", example = "1")
private Long parentUserId;
/**
* 上级用户列表1,2,3
*/
@Schema(description = "上级用户列表1,2,3", example = "${field.example}")
@Schema(description = "上级用户列表1,2,3", example = "1, 2, 3")
private String superUserList;