【发布时间】:2021-05-27 11:44:46
【问题描述】:
我目前正在构建一个 React/Springboot 应用程序,我的目标是从数组中返回单个对象,但对象在反应中无效,有谁知道如何从 JSON 数组中的对象中提取数据,或者如果有一种方法可以放入我的控制器中,将数组内的对象格式化为迷你数组?
{
- drinks: {
id: 1,
drinkId: null,
drinkName: "Bloody Mary",
glassType: "Highball",
strAlcoholic: "Alcoholic",
drinkDetails: "A Bloody Mary is a cocktail containing vodka, tomato juice, and other
spices and flavorings including Worcestershire sauce, hot sauces, garlic, herbs,
horseradish, celery, olives, salt, black pepper, lemon juice, lime juice and celery
salt.",
720x720-primary-28cf1aaa79d0424d951901fcc0a42e91_xmhgw9.jpg"
}
}
这是我对上述 json 数据的控制器:
@GetMapping(path = "all/{id}")
@CrossOrigin
public @ResponseBody Map<String, Optional<Drink>> getById(@PathVariable Long id){
Map<String, Optional<Drink>> response = new HashMap<>();
response.put("drinks", drinkRepository.findById(id));
return response;
}
【问题讨论】:
标签: java json reactjs spring-boot api