fix: 修复 ApiResponse 泛型类型不兼容问题
添加泛型版本的 fail 方法,支持返回指定类型的错误响应 更新 FileUploadController 使用新的 fail 方法指定 String 类型
This commit is contained in:
@@ -23,4 +23,8 @@ public class ApiResponse<T> {
|
||||
public static ApiResponse<Void> fail(String message) {
|
||||
return new ApiResponse<>(-1, message, null);
|
||||
}
|
||||
|
||||
public static <T> ApiResponse<T> fail(String message, Class<T> clazz) {
|
||||
return new ApiResponse<>(-1, message, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class FileUploadController {
|
||||
@PostMapping("/upload")
|
||||
public ApiResponse<String> 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<String> 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);
|
||||
|
||||
Reference in New Issue
Block a user