我试图把ArrayList<Burger>
为ArrayList<Object>
,但我得到这个错误。
我希望ArrayList<Object>
保持为对象,我如何才能完成这一点?
这是它为thisModelInfo.put(...);
The method put(String, ArrayList<Object>) in the type HashMap<String,ArrayList<Object>> is not applicable for the arguments (String, ArrayList<Burger>)
这是我的代码。
package com.ashgharibyan.apiofapis.controllers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ashgharibyan.apiofapis.models.Burger;
import com.ashgharibyan.apiofapis.models.User;
import com.ashgharibyan.apiofapis.services.BurgerService;
import com.ashgharibyan.apiofapis.services.UserService;
@RestController
public class UserAPIController {
@Autowired
private UserService userServ;
@Autowired
private BurgerService burgerServ;
@RequestMapping("/api/all")
public ArrayList<HashMap<String, ArrayList<HashMap<String, ArrayList<Object>>>>> index() {
List<User> allUsers = (List<User>) userServ.allUsers();
ArrayList<HashMap<String, ArrayList<HashMap<String, ArrayList<Object>>>>> mainAPI = new ArrayList<>();
for(int i=0; i<allUsers.size();i++) {
// One User Level
HashMap<String, ArrayList<HashMap<String, ArrayList<Object>>>> thisUserInfo = new HashMap<String, ArrayList<HashMap<String, ArrayList<Object>>>>();
//All Model Level
ArrayList<HashMap<String, ArrayList<Object>>> allModelInfo = new ArrayList<>();
//Current Model Level
HashMap<String, ArrayList<Object>> thisModelInfo = new HashMap<String, ArrayList<Object>>();
ArrayList<Burger> allBurgers = (ArrayList<Burger>) burgerServ.allBurgers();
thisModelInfo.put("burger", allBurgers);
allModelInfo.add(thisModelInfo);
thisUserInfo.put(allUsers.get(i).getUserName(), allModelInfo);
mainAPI.add(thisUserInfo);
}
return mainAPI;
}
}
我不知道该怎么做,使这一工作。我需要它是动态的,因为我将改变模型的名称,我正在导入。
1条答案
按热度按时间iyzzxitl1#
您可以将
ArrayList<Burger>
更改为ArrayList<Object>
或更改到