From 4877206761c3939859103e2b1e61d19272414fbd Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Wed, 30 Apr 2025 13:34:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/user/UserInfoController.java | 21 ++++++++- .../exception/GlobalExceptionHandler.java | 45 ++++++++++--------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/greenorange/promotion/controller/user/UserInfoController.java b/src/main/java/com/greenorange/promotion/controller/user/UserInfoController.java index 630af35..1ca04be 100644 --- a/src/main/java/com/greenorange/promotion/controller/user/UserInfoController.java +++ b/src/main/java/com/greenorange/promotion/controller/user/UserInfoController.java @@ -184,7 +184,7 @@ public class UserInfoController { @GetMapping("queryById") @Operation(summary = "web端管理员根据id查询用户", description = "参数:用户表查询请求体,权限:管理员(boss, admin),方法名:queryUserInfoById") // @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE) - public BaseResponse queryUserInfoByGetId(@RequestParam Long id) { + public BaseResponse 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 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 用户表批量删除请求体 diff --git a/src/main/java/com/greenorange/promotion/exception/GlobalExceptionHandler.java b/src/main/java/com/greenorange/promotion/exception/GlobalExceptionHandler.java index 60cc4c3..051d6c0 100644 --- a/src/main/java/com/greenorange/promotion/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/greenorange/promotion/exception/GlobalExceptionHandler.java @@ -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 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, "系统错误"); +// } }