参数校验
This commit is contained in:
parent
dc090de5ab
commit
99e002f054
|
@ -1,28 +1,22 @@
|
|||
package com.greenorange.promotion.annotation;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.greenorange.promotion.model.enums.UserRoleEnum;
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
|
||||
|
||||
// 枚举校验器
|
||||
public class EnumValidator implements ConstraintValidator<EnumValue, String> {
|
||||
|
||||
private EnumValue enumValue;
|
||||
|
||||
@Override
|
||||
public void initialize(EnumValue constraintAnnotation) {
|
||||
this.enumValue = constraintAnnotation;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||
if (value == null) {
|
||||
return true; // 如果值为 null,跳过校验,可以用 @NotNull 另行校验
|
||||
}
|
||||
|
||||
// 获取枚举类
|
||||
Class<? extends Enum<?>> enumClass = enumValue.enumClass();
|
||||
return Arrays.stream(enumClass.getEnumConstants())
|
||||
.anyMatch(enumConstant -> enumConstant.name().equals(value));
|
||||
return UserRoleEnum.getEnumByValues(value) != null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package com.greenorange.promotion.annotation;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import jakarta.validation.Constraint;
|
||||
import jakarta.validation.Payload;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
|
|
@ -166,7 +166,7 @@ public class UserInfoController {
|
|||
*/
|
||||
@PostMapping("queryById")
|
||||
@Operation(summary = "web端管理员根据id查询用户", description = "参数:用户表查询请求体,权限:管理员(boss, admin),方法名:queryUserInfoById")
|
||||
@RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
|
||||
// @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<UserInfoVO> queryUserInfoById(@Valid @RequestBody CommonRequest commonRequest) {
|
||||
Long id = commonRequest.getId();
|
||||
UserInfo userInfo = userInfoService.getById(id);
|
||||
|
@ -176,6 +176,23 @@ public class UserInfoController {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* web端管理员根据id查询用户表
|
||||
* @param id 用户表查询请求体
|
||||
* @return 用户表信息
|
||||
*/
|
||||
@GetMapping("queryById")
|
||||
@Operation(summary = "web端管理员根据id查询用户", description = "参数:用户表查询请求体,权限:管理员(boss, admin),方法名:queryUserInfoById")
|
||||
// @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<UserInfoVO> queryUserInfoByGetId(@RequestParam Long id) {
|
||||
UserInfo userInfo = userInfoService.getById(id);
|
||||
ThrowUtils.throwIf(userInfo == null, ErrorCode.OPERATION_ERROR, "当前用户不存在");
|
||||
UserInfoVO userInfoVO = commonService.copyProperties(userInfo, UserInfoVO.class);
|
||||
return ResultUtils.success(userInfoVO);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* web端管理员批量删除用户表
|
||||
* @param commonBatchRequest 用户表批量删除请求体
|
||||
|
|
|
@ -6,6 +6,7 @@ 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.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
|
@ -13,6 +14,7 @@ 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 org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
|
@ -43,6 +45,13 @@ public class GlobalExceptionHandler {
|
|||
}
|
||||
|
||||
|
||||
// // 处理参数类型不匹配的异常
|
||||
// @ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
||||
// public ResponseEntity<String> handleMethodArgumentTypeMismatch(MethodArgumentTypeMismatchException ex) {
|
||||
// return ResponseEntity.badRequest().body("Invalid value for parameter: " + ex.getName());
|
||||
// }
|
||||
|
||||
|
||||
// 处理消息体解析失败的异常
|
||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||
public BaseResponse<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
||||
|
|
|
@ -53,7 +53,7 @@ public class UserInfoAddRequest implements Serializable {
|
|||
*/
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@Size(min = 6, max = 10, message = "密码长度在 6 到 10 个字符")
|
||||
@Schema(description = "密码", example = "qingcheng")
|
||||
@Schema(description = "密码(建议加密存储)", example = "qingcheng")
|
||||
private String userPassword;
|
||||
|
||||
/**
|
||||
|
@ -67,7 +67,7 @@ public class UserInfoAddRequest implements Serializable {
|
|||
*/
|
||||
@EnumValue(enumClass = UserRoleEnum.class)
|
||||
@Schema(description = "用户角色", example = "USER")
|
||||
private UserRoleEnum userRole;
|
||||
private String userRole;
|
||||
|
||||
/**
|
||||
* 上级用户id
|
||||
|
@ -78,7 +78,7 @@ public class UserInfoAddRequest implements Serializable {
|
|||
/**
|
||||
* 上级用户列表(1,2,3)
|
||||
*/
|
||||
@Schema(description = "上级用户列表(1,2,3)", example = "1, 2, 3")
|
||||
@Schema(description = "上级用户列表(1,2,3)", example = "1,2,3")
|
||||
private String superUserList;
|
||||
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ public class UserInfoLoginRequest implements Serializable {
|
|||
*/
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@Size(min = 6, max = 10, message = "密码长度在 6 到 10 个字符")
|
||||
@Schema(description = "密码", example = "qingcheng")
|
||||
@Schema(description = "密码(建议加密存储)", example = "qingcheng")
|
||||
private String userPassword;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.greenorange.promotion.model.dto.user;
|
||||
|
||||
import com.greenorange.promotion.annotation.EnumValue;
|
||||
import com.greenorange.promotion.model.enums.UserRoleEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
@ -60,7 +62,7 @@ public class UserInfoUpdateRequest implements Serializable {
|
|||
*/
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@Size(min = 6, max = 10, message = "密码长度在 6 到 10 个字符")
|
||||
@Schema(description = "密码", example = "qingcheng")
|
||||
@Schema(description = "密码(建议加密存储)", example = "qingcheng")
|
||||
private String userPassword;
|
||||
|
||||
/**
|
||||
|
@ -72,7 +74,7 @@ public class UserInfoUpdateRequest implements Serializable {
|
|||
/**
|
||||
* 用户角色
|
||||
*/
|
||||
@NotBlank(message = "用户角色不能为空")
|
||||
@EnumValue(enumClass = UserRoleEnum.class)
|
||||
@Schema(description = "用户角色", example = "user")
|
||||
private String userRole;
|
||||
|
||||
|
@ -85,7 +87,7 @@ public class UserInfoUpdateRequest implements Serializable {
|
|||
/**
|
||||
* 上级用户列表(1,2,3)
|
||||
*/
|
||||
@Schema(description = "上级用户列表(1,2,3)", example = "1, 2, 3")
|
||||
@Schema(description = "上级用户列表(1,2,3)", example = "1,2,3")
|
||||
private String superUserList;
|
||||
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ public class UserInfo implements Serializable {
|
|||
private String userAccount;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
* 密码(建议加密存储)
|
||||
*/
|
||||
private String userPassword;
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.greenorange.promotion.model.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
@ -39,7 +40,7 @@ public enum UserRoleEnum {
|
|||
* 获取值列表
|
||||
*/
|
||||
public static UserRoleEnum getEnumByValues(String value) {
|
||||
if (ObjectUtils.isEmpty(value)) {
|
||||
if (StringUtils.isBlank(value)) {
|
||||
return null;
|
||||
}
|
||||
for (UserRoleEnum anEnum : UserRoleEnum.values()) {
|
||||
|
@ -49,4 +50,6 @@ public enum UserRoleEnum {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.greenorange.promotion.model.vo.user;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serial;
|
||||
|
@ -16,55 +18,63 @@ public class UserInfoVO implements Serializable {
|
|||
/**
|
||||
* 用户表 ID
|
||||
*/
|
||||
@Schema(description = "用户表 ID", example = "1")
|
||||
@Schema(description = "用户ID", example = "1")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 用户昵称
|
||||
*/
|
||||
@Schema(description = "用户昵称", example = "${field.example}")
|
||||
@Schema(description = "用户昵称", example = "chenxinzhi")
|
||||
private String nickName;
|
||||
|
||||
/**
|
||||
* 用户头像URL
|
||||
*/
|
||||
@Schema(description = "用户头像URL", example = "${field.example}")
|
||||
@Schema(description = "用户头像URL", example = "http://xxx.png")
|
||||
private String userAvatar;
|
||||
|
||||
/**
|
||||
* 手机号
|
||||
*/
|
||||
@Schema(description = "手机号", example = "${field.example}")
|
||||
@Schema(description = "手机号", example = "15888610253")
|
||||
private String phoneNumber;
|
||||
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
@Schema(description = "账号", example = "qingcheng")
|
||||
private String userAccount;
|
||||
|
||||
|
||||
/**
|
||||
* 密码(建议加密存储)
|
||||
*/
|
||||
@Schema(description = "密码(建议加密存储)", example = "${field.example}")
|
||||
@Schema(description = "密码(建议加密存储)", example = "qingcheng")
|
||||
private String userPassword;
|
||||
|
||||
/**
|
||||
* 邀请码
|
||||
*/
|
||||
@Schema(description = "邀请码", example = "${field.example}")
|
||||
@Schema(description = "邀请码", example = "666999")
|
||||
private String invitationCode;
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
*/
|
||||
@Schema(description = "用户角色", example = "${field.example}")
|
||||
@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;
|
||||
|
||||
|
||||
|
|
15
src/test/java/com/greenorange/promotion/EnumTest.java
Normal file
15
src/test/java/com/greenorange/promotion/EnumTest.java
Normal file
|
@ -0,0 +1,15 @@
|
|||
package com.greenorange.promotion;
|
||||
|
||||
import com.greenorange.promotion.model.enums.UserRoleEnum;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class EnumTest {
|
||||
|
||||
@Test
|
||||
public void testEnum() {
|
||||
UserRoleEnum admin = UserRoleEnum.ADMIN;
|
||||
String name = admin.name();
|
||||
System.out.println(name);
|
||||
}
|
||||
|
||||
}
|
59
src/test/java/com/greenorange/promotion/FactoryDemo.java
Normal file
59
src/test/java/com/greenorange/promotion/FactoryDemo.java
Normal file
|
@ -0,0 +1,59 @@
|
|||
package com.greenorange.promotion;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class FactoryDemo {
|
||||
|
||||
@Test
|
||||
public void ClientTest() {
|
||||
CarFactory bmwFactory = new BMWFactory();
|
||||
Car bmw = bmwFactory.createCar();
|
||||
bmw.drive(); // 输出: Driving a BMW car
|
||||
|
||||
CarFactory benzFactory = new BenzFactory();
|
||||
Car benz = benzFactory.createCar();
|
||||
benz.drive(); // 输出: Driving a Benz car
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
interface Car {
|
||||
void drive();
|
||||
}
|
||||
|
||||
class BMWCar implements Car {
|
||||
@Override
|
||||
public void drive() {
|
||||
System.out.println("Driving a BMW car");
|
||||
}
|
||||
}
|
||||
|
||||
class BenzCar implements Car {
|
||||
@Override
|
||||
public void drive() {
|
||||
System.out.println("Driving a Benz car");
|
||||
}
|
||||
}
|
||||
|
||||
interface CarFactory {
|
||||
Car createCar();
|
||||
}
|
||||
|
||||
|
||||
class BMWFactory implements CarFactory {
|
||||
@Override
|
||||
public Car createCar() {
|
||||
return new BMWCar();
|
||||
}
|
||||
}
|
||||
|
||||
class BenzFactory implements CarFactory {
|
||||
@Override
|
||||
public Car createCar() {
|
||||
return new BenzCar();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user