fix: 修复食谱收藏接口 ClassCastException 错误
- 修复 toggleRecipeFavorite 方法中 recipeId 类型转换问题 - 支持 Number 和 String 两种类型传入 - 添加参数校验,返回友好的错误提示 Error: java.lang.String cannot be cast to java.lang.Number
This commit is contained in:
@@ -583,7 +583,15 @@ public class ToolController {
|
||||
@ApiOperation(value = "收藏/取消收藏食谱")
|
||||
@PostMapping("/recipe/favorite")
|
||||
public CommonResult<String> toggleRecipeFavorite(@RequestBody Map<String, Object> data) {
|
||||
Long recipeId = ((Number) data.get("recipeId")).longValue();
|
||||
Object recipeIdObj = data.get("recipeId");
|
||||
Long recipeId;
|
||||
if (recipeIdObj instanceof Number) {
|
||||
recipeId = ((Number) recipeIdObj).longValue();
|
||||
} else if (recipeIdObj instanceof String) {
|
||||
recipeId = Long.parseLong((String) recipeIdObj);
|
||||
} else {
|
||||
return CommonResult.failed("食谱ID格式错误");
|
||||
}
|
||||
Boolean isFavorite = (Boolean) data.get("isFavorite");
|
||||
toolRecipeService.toggleFavorite(recipeId, isFavorite);
|
||||
return CommonResult.success("操作成功");
|
||||
|
||||
Reference in New Issue
Block a user