参数校验
This commit is contained in:
parent
99e002f054
commit
4877206761
|
@ -184,7 +184,7 @@ public class UserInfoController {
|
||||||
@GetMapping("queryById")
|
@GetMapping("queryById")
|
||||||
@Operation(summary = "web端管理员根据id查询用户", description = "参数:用户表查询请求体,权限:管理员(boss, admin),方法名:queryUserInfoById")
|
@Operation(summary = "web端管理员根据id查询用户", description = "参数:用户表查询请求体,权限:管理员(boss, admin),方法名:queryUserInfoById")
|
||||||
// @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
|
// @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);
|
UserInfo userInfo = userInfoService.getById(id);
|
||||||
ThrowUtils.throwIf(userInfo == null, ErrorCode.OPERATION_ERROR, "当前用户不存在");
|
ThrowUtils.throwIf(userInfo == null, ErrorCode.OPERATION_ERROR, "当前用户不存在");
|
||||||
UserInfoVO userInfoVO = commonService.copyProperties(userInfo, UserInfoVO.class);
|
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端管理员批量删除用户表
|
* web端管理员批量删除用户表
|
||||||
* @param commonBatchRequest 用户表批量删除请求体
|
* @param commonBatchRequest 用户表批量删除请求体
|
||||||
|
|
|
@ -29,6 +29,21 @@ import java.util.List;
|
||||||
public class GlobalExceptionHandler {
|
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)
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
public BaseResponse<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
public BaseResponse<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||||
|
@ -36,29 +51,15 @@ public class GlobalExceptionHandler {
|
||||||
// 按字段名排序,确保每次返回的顺序一致
|
// 按字段名排序,确保每次返回的顺序一致
|
||||||
e.getBindingResult().getFieldErrors().stream()
|
e.getBindingResult().getFieldErrors().stream()
|
||||||
.sorted(Comparator.comparing(FieldError::getField)) // 按字段名排序
|
.sorted(Comparator.comparing(FieldError::getField)) // 按字段名排序
|
||||||
.forEach(fieldError -> errors.append("Field: ")
|
.forEach(fieldError -> errors.append("参数: ")
|
||||||
.append(fieldError.getField())
|
.append(fieldError.getField())
|
||||||
.append(" | Error: ")
|
.append(" | 错误: ")
|
||||||
.append(fieldError.getDefaultMessage())
|
.append(fieldError.getDefaultMessage())
|
||||||
.append("; "));
|
.append("; "));
|
||||||
return ResultUtils.error(ErrorCode.PARAMS_ERROR, errors.toString());
|
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)
|
@ExceptionHandler(BusinessException.class)
|
||||||
|
@ -68,11 +69,11 @@ public class GlobalExceptionHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 处理运行时异常
|
// // 处理运行时异常
|
||||||
@ExceptionHandler(RuntimeException.class)
|
// @ExceptionHandler(RuntimeException.class)
|
||||||
public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
|
// public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
|
||||||
log.error("RuntimeException", e);
|
// log.error("RuntimeException", e);
|
||||||
return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");
|
// return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user