参数校验
This commit is contained in:
parent
f36e4a38a8
commit
e30312c743
|
@ -5,12 +5,19 @@ import com.greenorange.promotion.common.ErrorCode;
|
||||||
import com.greenorange.promotion.common.ResultUtils;
|
import com.greenorange.promotion.common.ResultUtils;
|
||||||
import io.swagger.v3.oas.annotations.Hidden;
|
import io.swagger.v3.oas.annotations.Hidden;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.sf.jsqlparser.util.validation.ValidationError;
|
||||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.validation.FieldError;
|
||||||
import org.springframework.validation.ObjectError;
|
import org.springframework.validation.ObjectError;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
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)
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
public BaseResponse<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
public BaseResponse<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||||
log.error("MethodArgumentNotValidException", e);
|
StringBuilder errors = new StringBuilder();
|
||||||
return ResultUtils.error(ErrorCode.PARAMS_ERROR, e.getMessage());
|
// 按字段名排序,确保每次返回的顺序一致
|
||||||
|
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)
|
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||||
public BaseResponse<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
public BaseResponse<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
||||||
|
|
|
@ -2,7 +2,9 @@ package com.greenorange.promotion.model.dto.user;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@ -11,41 +13,41 @@ import java.io.Serializable;
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@Schema(description = "用户表添加请求体", requiredProperties = {"nickName", "userAvatar", "phoneNumber",
|
@Schema(description = "用户表添加请求体", requiredProperties = {"nickName", "userAvatar", "phoneNumber",
|
||||||
"userAccount", "userPassword", "invitationCode", "userRole", "parentUserId", "superUserList"})
|
"userAccount", "userPassword", "userRole"})
|
||||||
public class UserInfoAddRequest implements Serializable {
|
public class UserInfoAddRequest implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户昵称
|
* 用户昵称
|
||||||
*/
|
*/
|
||||||
@NotBlank(message = "参数不能为空")
|
@NotBlank(message = "用户昵称不能为空")
|
||||||
@Schema(description = "用户昵称", example = "chenxinzhi")
|
@Schema(description = "用户昵称", example = "chenxinzhi")
|
||||||
private String nickName;
|
private String nickName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户头像URL
|
* 用户头像URL
|
||||||
*/
|
*/
|
||||||
@NotBlank(message = "参数不能为空")
|
@NotBlank(message = "用户头像URL不能为空")
|
||||||
@Schema(description = "用户头像URL", example = "http://xxx.png")
|
@Schema(description = "用户头像URL", example = "http://xxx.png")
|
||||||
private String userAvatar;
|
private String userAvatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 手机号
|
* 手机号
|
||||||
*/
|
*/
|
||||||
@NotBlank(message = "参数不能为空")
|
@NotBlank(message = "手机号不能为空")
|
||||||
@Schema(description = "手机号", example = "15888610253")
|
@Schema(description = "手机号", example = "15888610253")
|
||||||
private String phoneNumber;
|
private String phoneNumber;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 账号
|
* 账号
|
||||||
*/
|
*/
|
||||||
@NotBlank(message = "参数不能为空")
|
@NotBlank(message = "账号不能为空")
|
||||||
@Schema(description = "账号", example = "qingcheng_account")
|
@Schema(description = "账号", example = "qingcheng_account")
|
||||||
private String userAccount;
|
private String userAccount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 密码
|
* 密码
|
||||||
*/
|
*/
|
||||||
@NotBlank(message = "参数不能为空")
|
@NotBlank(message = "密码不能为空")
|
||||||
@Schema(description = "密码", example = "qingcheng_password")
|
@Schema(description = "密码", example = "qingcheng_password")
|
||||||
private String userPassword;
|
private String userPassword;
|
||||||
|
|
||||||
|
@ -58,7 +60,7 @@ public class UserInfoAddRequest implements Serializable {
|
||||||
/**
|
/**
|
||||||
* 用户角色
|
* 用户角色
|
||||||
*/
|
*/
|
||||||
@NotBlank(message = "参数不能为空")
|
@NotBlank(message = "用户角色不能为空")
|
||||||
@Schema(description = "用户角色", example = "user")
|
@Schema(description = "用户角色", example = "user")
|
||||||
private String userRole;
|
private String userRole;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.greenorange.promotion.model.dto.user;
|
package com.greenorange.promotion.model.dto.user;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
@ -15,12 +16,14 @@ public class UserInfoLoginRequest implements Serializable {
|
||||||
/**
|
/**
|
||||||
* 账号
|
* 账号
|
||||||
*/
|
*/
|
||||||
|
@NotBlank(message = "账号不能为空")
|
||||||
@Schema(description = "账号", example = "${field.example}")
|
@Schema(description = "账号", example = "${field.example}")
|
||||||
private String userAccount;
|
private String userAccount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 密码
|
* 密码
|
||||||
*/
|
*/
|
||||||
|
@NotBlank(message = "密码不能为空")
|
||||||
@Schema(description = "密码", example = "${field.example}")
|
@Schema(description = "密码", example = "${field.example}")
|
||||||
private String userPassword;
|
private String userPassword;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package com.greenorange.promotion.model.dto.user;
|
package com.greenorange.promotion.model.dto.user;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
@ -14,14 +16,16 @@ import com.greenorange.promotion.common.PageRequest;
|
||||||
public class UserInfoQueryRequest extends PageRequest implements Serializable {
|
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;
|
private Long id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 手机号
|
* 手机号
|
||||||
*/
|
*/
|
||||||
|
@NotBlank(message = "手机号不能为空")
|
||||||
@Schema(description = "手机号", example = "${field.example}")
|
@Schema(description = "手机号", example = "${field.example}")
|
||||||
private String phoneNumber;
|
private String phoneNumber;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package com.greenorange.promotion.model.dto.user;
|
package com.greenorange.promotion.model.dto.user;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
|
@ -11,67 +13,74 @@ import java.io.Serializable;
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@Schema(description = "用户表更新请求体", requiredProperties = {"id", "nickName", "userAvatar", "phoneNumber",
|
@Schema(description = "用户表更新请求体", requiredProperties = {"id", "nickName", "userAvatar", "phoneNumber",
|
||||||
"userAccount", "userPassword", "invitationCode", "userRole", "parentUserId", "superUserList"})
|
"userAccount", "userPassword", "userRole"})
|
||||||
public class UserInfoUpdateRequest implements Serializable {
|
public class UserInfoUpdateRequest implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户表 ID
|
* 用户ID
|
||||||
*/
|
*/
|
||||||
@Schema(description = "用户表 ID", example = "1")
|
@NotNull(message = "用户ID不能为null")
|
||||||
|
@Schema(description = "用户ID", example = "1")
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户昵称
|
* 用户昵称
|
||||||
*/
|
*/
|
||||||
@Schema(description = "用户昵称", example = "${field.example}")
|
@NotBlank(message = "用户昵称不能为空")
|
||||||
|
@Schema(description = "用户昵称", example = "chenxinzhi")
|
||||||
private String nickName;
|
private String nickName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户头像URL
|
* 用户头像URL
|
||||||
*/
|
*/
|
||||||
@Schema(description = "用户头像URL", example = "${field.example}")
|
@NotBlank(message = "用户头像URL不能为空")
|
||||||
|
@Schema(description = "用户头像URL", example = "http://xxx.png")
|
||||||
private String userAvatar;
|
private String userAvatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 手机号
|
* 手机号
|
||||||
*/
|
*/
|
||||||
@Schema(description = "手机号", example = "${field.example}")
|
@NotBlank(message = "手机号不能为空")
|
||||||
|
@Schema(description = "手机号", example = "15888610253")
|
||||||
private String phoneNumber;
|
private String phoneNumber;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 账号
|
* 账号
|
||||||
*/
|
*/
|
||||||
@Schema(description = "账号", example = "${field.example}")
|
@NotBlank(message = "账号不能为空")
|
||||||
|
@Schema(description = "账号", example = "qingcheng_account")
|
||||||
private String userAccount;
|
private String userAccount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 密码
|
* 密码
|
||||||
*/
|
*/
|
||||||
@Schema(description = "密码", example = "${field.example}")
|
@NotBlank(message = "密码不能为空")
|
||||||
|
@Schema(description = "密码", example = "qingcheng_password")
|
||||||
private String userPassword;
|
private String userPassword;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 邀请码
|
* 邀请码
|
||||||
*/
|
*/
|
||||||
@Schema(description = "邀请码", example = "${field.example}")
|
@Schema(description = "邀请码", example = "666999")
|
||||||
private String invitationCode;
|
private String invitationCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户角色
|
* 用户角色
|
||||||
*/
|
*/
|
||||||
@Schema(description = "用户角色", example = "${field.example}")
|
@NotBlank(message = "用户角色不能为空")
|
||||||
|
@Schema(description = "用户角色", example = "user")
|
||||||
private String userRole;
|
private String userRole;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上级用户id
|
* 上级用户id
|
||||||
*/
|
*/
|
||||||
@Schema(description = "上级用户id", example = "${field.example}")
|
@Schema(description = "上级用户id", example = "1")
|
||||||
private Long parentUserId;
|
private Long parentUserId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上级用户列表(1,2,3)
|
* 上级用户列表(1,2,3)
|
||||||
*/
|
*/
|
||||||
@Schema(description = "上级用户列表(1,2,3)", example = "${field.example}")
|
@Schema(description = "上级用户列表(1,2,3)", example = "1, 2, 3")
|
||||||
private String superUserList;
|
private String superUserList;
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user