From 283f7465bcf376763e8b17136b243f3a9a9c2490 Mon Sep 17 00:00:00 2001 From: wangziqi Date: Tue, 10 Feb 2026 15:45:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20ApiResponse=20?= =?UTF-8?q?=E6=B3=9B=E5=9E=8B=E7=B1=BB=E5=9E=8B=E4=B8=8D=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加泛型版本的 fail 方法,支持返回指定类型的错误响应 更新 FileUploadController 使用新的 fail 方法指定 String 类型 --- .../main/java/com/maternalmall/common/ApiResponse.java | 4 ++++ .../com/maternalmall/controller/FileUploadController.java | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/com/maternalmall/common/ApiResponse.java b/backend/src/main/java/com/maternalmall/common/ApiResponse.java index f24a2de..6a3a276 100644 --- a/backend/src/main/java/com/maternalmall/common/ApiResponse.java +++ b/backend/src/main/java/com/maternalmall/common/ApiResponse.java @@ -23,4 +23,8 @@ public class ApiResponse { public static ApiResponse fail(String message) { return new ApiResponse<>(-1, message, null); } + + public static ApiResponse fail(String message, Class clazz) { + return new ApiResponse<>(-1, message, null); + } } diff --git a/backend/src/main/java/com/maternalmall/controller/FileUploadController.java b/backend/src/main/java/com/maternalmall/controller/FileUploadController.java index b2ecd3f..09bf48e 100644 --- a/backend/src/main/java/com/maternalmall/controller/FileUploadController.java +++ b/backend/src/main/java/com/maternalmall/controller/FileUploadController.java @@ -25,7 +25,7 @@ public class FileUploadController { @PostMapping("/upload") public ApiResponse uploadFile(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { - return ApiResponse.fail("请选择要上传的文件"); + return ApiResponse.fail("请选择要上传的文件", String.class); } try { @@ -52,20 +52,20 @@ public class FileUploadController { return ApiResponse.ok(fileUrl); } catch (IOException e) { - return ApiResponse.fail("文件上传失败: " + e.getMessage()); + return ApiResponse.fail("文件上传失败: " + e.getMessage(), String.class); } } @PostMapping("/upload/image") public ApiResponse uploadImage(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { - return ApiResponse.fail("请选择要上传的图片"); + return ApiResponse.fail("请选择要上传的图片", String.class); } // 检查文件类型 String contentType = file.getContentType(); if (contentType == null || !contentType.startsWith("image/")) { - return ApiResponse.fail("只能上传图片文件"); + return ApiResponse.fail("只能上传图片文件", String.class); } return uploadFile(file);