[英]Spring mongo data repository findAll() return empty?
I'm a newbie in Spring Framework.我是 Spring Framework 的新手。 I'm trying to create a small REST API by using Spring Rest with MongoDB.
我正在尝试通过使用 Spring Rest 和 MongoDB 创建一个小的 REST API。 And when I create the repository to get data from MongoDb, the findAll() function always return an empty list.
当我创建存储库以从 MongoDb 获取数据时,findAll() 函数总是返回一个空列表。 Here is my repository:
这是我的存储库:
@RepositoryRestResource(collectionResourceRel = "meal", path = "meal")
public interface MealRepository extends MongoRepository<Meal, Integer> {
@Override
public List<Meal> findAll();
@Override
public Meal findOne(Integer id);
}
And Controller:和控制器:
@RestController
public class MealApiController {
@Autowired
MealRepository mMealRepository;
@RequestMapping(value = "/meal/detail", method = RequestMethod.GET)
public @ResponseBody Meal mealDetailGet(@RequestParam(value = "id", required = true) Integer id) {
Meal meal = mMealRepository.findOne(id);
return meal;
}
@RequestMapping(value = "/meal/all", method = RequestMethod.GET)
public @ResponseBody List<Meal> getAllMeal() {
return mMealRepository.findAll();
}
@RequestMapping(value = "/meal/list", method = RequestMethod.GET)
public @ResponseBody List<Meal> mealListGet(@RequestParam(value = "menu_id", required = true) Integer menuId) {
List<Meal> response = mMealRepository.findByMenuId(menuId);
return response;
}
}
Meal model:餐型:
@Document(collection = "meal_items")
public class Meal {
@Id
@JsonProperty("_id")
private int id;
@JsonProperty("menu_id")
private int menuId;
@JsonProperty("name")
private String name = null;
@JsonProperty("image")
private List<String> image = new ArrayList<String>();
@JsonProperty("material")
private List<String> material = new ArrayList<String>();
@JsonProperty("guide")
private List<String> guide = new ArrayList<String>();
public Meal id(int id) {
this.id = id;
return this;
}
/**
* Unique identifier representing a specific Meal.
*
* @return id
**/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Meal menuId(int menuId) {
this.menuId = menuId;
return this;
}
/**
* Unique identifier representing a specific Menu containing the meal.
*
* @return menuId
**/
public int getMenuId() {
return menuId;
}
public void setMenuId(int menuId) {
this.menuId = menuId;
}
public Meal name(String name) {
this.name = name;
return this;
}
/**
* Display name of meal.
*
* @return name
**/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Meal image(List<String> image) {
this.image = image;
return this;
}
public Meal addImageItem(String imageItem) {
this.image.add(imageItem);
return this;
}
/**
* Image URL representing the meal.
*
* @return image
**/
public List<String> getImage() {
return image;
}
public void setImage(List<String> image) {
this.image = image;
}
public Meal material(List<String> material) {
this.material = material;
return this;
}
public Meal addMaterialItem(String materialItem) {
this.material.add(materialItem);
return this;
}
/**
* List material used to cook the meal.
*
* @return material
**/
public List<String> getMaterial() {
return material;
}
public void setMaterial(List<String> material) {
this.material = material;
}
public Meal guide(List<String> guide) {
this.guide = guide;
return this;
}
public Meal addGuideItem(String guideItem) {
this.guide.add(guideItem);
return this;
}
/**
* Steps cooking the meal.
*
* @return guide
**/
public List<String> getGuide() {
return guide;
}
public void setGuide(List<String> guide) {
this.guide = guide;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Meal meal = (Meal) o;
return Objects.equals(this.id, meal.id) && Objects.equals(this.menuId, meal.menuId)
&& Objects.equals(this.name, meal.name) && Objects.equals(this.image, meal.image)
&& Objects.equals(this.material, meal.material) && Objects.equals(this.guide, meal.guide);
}
@Override
public int hashCode() {
return Objects.hash(id, menuId, name, image, material, guide);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Meal {\n");
sb.append(" id: ").append(toIndentedString(id)).append("\n");
sb.append(" menuId: ").append(toIndentedString(menuId)).append("\n");
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append(" image: ").append(toIndentedString(image)).append("\n");
sb.append(" material: ").append(toIndentedString(material)).append("\n");
sb.append(" guide: ").append(toIndentedString(guide)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
A couple of things.几件事。
Firstly, this behaviour is not reproducible.首先,这种行为是不可重现的。 It works fine.
它工作正常。
Secondly, you don't need a separate controller( @RestController
) class to expose your REST endpoints(unless needed for specific reasons).其次,您不需要单独的控制器(
@RestController
)类来公开您的 REST 端点(除非出于特定原因需要)。 When you use spring-data-rest( @RepositoryRestResource
), spring exposes endpoints for you, including endpoints for custom querymethods like findByMenuId
.当您使用 spring-data-rest(
@RepositoryRestResource
) 时,spring 会为您公开端点,包括自定义查询方法的端点,例如findByMenuId
。
In your example, just delete the controller class and restart the application.在您的示例中,只需删除控制器类并重新启动应用程序。 And try to hit http://host:port/meal from the browser, it will display all the possible options as links, which is self-explanatory.
并尝试从浏览器点击http://host:port/meal ,它将显示所有可能的选项作为链接,这是不言自明的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.