Java 所需的字符串参数“foodId”不存在
我试图通过foodId获取一个food对象,但得到400个错误代码,表示所需的字符串参数“foodId”不存在 这是我的控制器类:Java 所需的字符串参数“foodId”不存在,java,mongodb,spring-mongodb,Java,Mongodb,Spring Mongodb,我试图通过foodId获取一个food对象,但得到400个错误代码,表示所需的字符串参数“foodId”不存在 这是我的控制器类: package com.quickmeal.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.R
package com.quickmeal.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
@RequestMapping("/api/foods")
public class FoodController {
@Autowired
private FoodService foodService;
@RequestMapping(method = RequestMethod.POST)
public Food createFood(@RequestBody Food food) {
return foodService.createFood(food);
}
@RequestMapping(method = RequestMethod.GET)
public List<Food> listFood() {
return foodService.listFood();
}
@RequestMapping(value = "/{foodId}",method = RequestMethod.GET)
public Object getFoodById(@RequestParam String foodId) {
System.out.println("Testing..."+foodId);
return foodService.getFoodById(foodId);
}
}
这是我的网址
本地主机:8080/api/foods/5b08f8cee731e32c60e95aa8
5b08f8cee731e32c60e95aa8是mongoDB提供的id
请帮忙。谢谢。此路径中的foodId/{foodId}是@PathVariable而不是@RequestParam
正确的处理方法应为:
@RequestMapping(value = "/{foodId}",method = RequestMethod.GET)
public Object getFoodById(@PathVariable String foodId) {
package com.quickmeal.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Food {
@Id
private String foodId;
private String foodName;
private double foodPrice;
public String getFoodId() {
return foodId;
}
public void setFoodId(String foodId) {
this.foodId = foodId;
}
public String getFoodName() {
return foodName;
}
public void setFoodName(String foodName) {
this.foodName = foodName;
}
public double getFoodPrice() {
return foodPrice;
}
public void setFoodPrice(double foodPrice) {
this.foodPrice = foodPrice;
}
}
@RequestMapping(value = "/{foodId}",method = RequestMethod.GET)
public Object getFoodById(@PathVariable String foodId) {