youngsad 2022-05-19 05:39 采纳率: 25%
浏览 17
已结题

如何创建一个SpringBoot API, 使用data.json做为数据源

 这里我有一个json数据,data.json
{
"recipes": [
{
"name": "scrambledEggs",
"ingredients": [
"1 tsp oil",
"2 eggs",
"salt"
],
"instructions": [
"Beat eggs with salt",
"Heat oil in pan",
"Add eggs to pan when hot",
"Gather eggs into curds, remove when cooked",
"Salt to taste and enjoy"
]
},
{
"name": "garlicPasta",
"ingredients": [
"500mL water",
"100g spaghetti",
"25mL olive oil",
"4 cloves garlic",
"Salt"
],
"instructions": [
"Heat garlic in olive oil",
"Boil water in pot",
"Add pasta to boiling water",
"Remove pasta from water and mix with garlic olive oil",
"Salt to taste and enjoy"
]
},
{
"name": "chai",
"ingredients": [
"400mL water",
"100mL milk",
"5g chai masala",
"2 tea bags or 20 g loose tea leaves"
],
"instructions": [
"Heat water until 80 C",
"Add milk, heat until 80 C",
"Add tea leaves/tea bags, chai masala; mix and steep for 3-4 minutes",
"Remove mixture from heat; strain and enjoy"
]
}
]
}

创建一个SpringBoot的api,要求如下
part 1
A GET request to http://localhost:3000/recipes returns:
Response body (JSON):
{
"recipeNames":
[
"scrambledEggs",
"garlicPasta",
"chai"
]
}
Status: 200

part 2
A GET request to http://localhost:3000/recipes/details/garlicPasta returns:
If recipe exists:
Response body (JSON):
{
"details":
{
"ingredients": [
"500mL water",
"100g spaghetti",
"25mL olive oil",
"4 cloves garlic",
"Salt"
],
"numSteps":5
}
}
Status: 200


If recipe does NOT exist:
Response body (JSON): {}
Status: 200

part 3
A POST request to http://localhost:3000/recipes with body
{
"name": "butteredBagel",
"ingredients": [
"1 bagel",
"butter"
],
"instructions": [
"cut the bagel",
"spread butter on bagel"
]
}
returns:
Response body: None
Status: 201
// 已存在
Response body (JSON):
{
"error": "Recipe already exists"
}
Status: 400

part 4
A PUT request to http://localhost:3000/recipes with body
{
"name": "butteredBagel",
"ingredients": [
"1 bagel",
"2 tbsp butter"
],
"instructions": [
"cut the bagel",
"spread butter on bagel"
]
} returns:
Response body: None
Status: 204
//不存在
Response body (JSON):
{
"error": "Recipe does not exist"
}
Status: 404

  • 写回答

1条回答 默认 最新

  • windcore 2022-05-19 07:48
    关注

    其实就是用文件操作即可,读取json文件内容,反序列化为一个Hashmap,然后判断是否有对应键值

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月3日
  • 已采纳回答 11月25日
  • 创建了问题 5月19日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么