2017-03-16 73 views
0

我是Spring框架的新手。我試圖通過使用Spring Rest和MongoDB來創建一個小的REST API。 當我創建存儲庫以從MongoDb獲取數據時,findAll()函數總是返回一個空列表。這裏是我的倉庫:Spring mongo數據存儲庫findAll()返回空嗎?

@RepositoryRestResource(collectionResourceRel = "meal", path = "meal") 
    public interface MealRepository extends MongoRepository<Meal, Integer> { 

     @Override 
     public List<Meal> findAll(); 

     @Override 
     public Meal findOne(Integer id); 
    } 

和控制器:

@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; 
    } 

} 

膳食模式:

@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 "); 
    } 
} 
+0

不需要重寫findAll()和findOne()方法,因爲它們已經分別在MongoRepository和CrudRepository中定義。 – dsank

+0

刪除這些方法並沒有幫我 –

+0

請附上實體(Meal)和配置mongo倉庫類 – dsank

回答

0

幾件事情。
首先,這種行爲是不可重現的。它工作正常。其次,您不需要單獨的控制器(@RestController)類來公開您的REST端點(除非由於特定原因需要)。當您使用spring-data-rest(@RepositoryRestResource)時,spring會爲您顯示端點,包括自定義查詢方法(如findByMenuId)的端點。
在你的例子中,只需刪除控制器類並重新啓動應用程序。並嘗試從瀏覽器中點擊http://host:port/meal,它會將所有可能的選項顯示爲鏈接,這是不言自明的。

+0

非常感謝。它幫助我很多 –

相關問題