参数校验

This commit is contained in:
chen-xin-zhi 2025-04-30 13:34:09 +08:00
parent 99e002f054
commit 4877206761
2 changed files with 43 additions and 23 deletions

View File

@ -184,7 +184,7 @@ public class UserInfoController {
@GetMapping("queryById")
@Operation(summary = "web端管理员根据id查询用户", description = "参数用户表查询请求体权限管理员boss, admin),方法名:queryUserInfoById")
// @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<UserInfoVO> queryUserInfoByGetId(@RequestParam Long id) {
public BaseResponse<UserInfoVO> queryUserInfoByGetId(@RequestParam Long id, @RequestParam Long path) {
UserInfo userInfo = userInfoService.getById(id);
ThrowUtils.throwIf(userInfo == null, ErrorCode.OPERATION_ERROR, "当前用户不存在");
UserInfoVO userInfoVO = commonService.copyProperties(userInfo, UserInfoVO.class);
@ -193,6 +193,25 @@ public class UserInfoController {
/**
* web端管理员根据id查询用户表
* @param id 用户表查询请求体
* @return 用户表信息
*/
@GetMapping("queryById/{id}")
@Operation(summary = "web端管理员根据id查询用户", description = "参数用户表查询请求体权限管理员boss, admin),方法名:queryUserInfoById")
// @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<UserInfoVO> queryUserInfoByPathId(@PathVariable 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 用户表批量删除请求体

View File

@ -29,6 +29,21 @@ import java.util.List;
public class GlobalExceptionHandler {
// 处理参数类型不匹配的异常GET请求
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public BaseResponse<?> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) {
return ResultUtils.error(ErrorCode.PARAMS_ERROR, "请求参数类型不匹配: " + ex.getName());
}
// 处理消息体解析失败的异常POST请求
@ExceptionHandler(HttpMessageNotReadableException.class)
public BaseResponse<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
log.error("HttpMessageNotReadableException", e);
return ResultUtils.error(ErrorCode.PARAMS_ERROR, "请求体不能为空或格式无效");
}
// 处理参数绑定失败的异常
@ExceptionHandler(MethodArgumentNotValidException.class)
public BaseResponse<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
@ -36,29 +51,15 @@ public class GlobalExceptionHandler {
// 按字段名排序确保每次返回的顺序一致
e.getBindingResult().getFieldErrors().stream()
.sorted(Comparator.comparing(FieldError::getField)) // 按字段名排序
.forEach(fieldError -> errors.append("Field: ")
.forEach(fieldError -> errors.append("参数: ")
.append(fieldError.getField())
.append(" | Error: ")
.append(" | 错误: ")
.append(fieldError.getDefaultMessage())
.append("; "));
return ResultUtils.error(ErrorCode.PARAMS_ERROR, errors.toString());
}
// // 处理参数类型不匹配的异常
// @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) {
log.error("HttpMessageNotReadableException", e);
return ResultUtils.error(ErrorCode.PARAMS_ERROR, "请求体不能为空或格式无效");
}
// 处理业务异常
@ExceptionHandler(BusinessException.class)
@ -68,11 +69,11 @@ public class GlobalExceptionHandler {
}
// 处理运行时异常
@ExceptionHandler(RuntimeException.class)
public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
log.error("RuntimeException", e);
return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");
}
// // 处理运行时异常
// @ExceptionHandler(RuntimeException.class)
// public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
// log.error("RuntimeException", e);
// return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");
// }
}