2017-03-16 16 views
0

私はSpring Frameworkの初心者です。私はMongoDBでSpring Restを使用して小さなREST APIを作成しようとしています。 MongoDbからデータを取得するためにリポジトリを作成すると、findAll()関数は常に空のリストを返します。春の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

既にMongoRepositoryとCrudRepositoryで定義されているので、findAll()メソッドとfindOne()メソッドをオーバーライドする必要はありません。 – dsank

+0

これらのメソッドを削除しても助けにならなかった –

+0

エンティティ(食事)とコンフィグレーションmongoリポジトリクラスを添付してください – dsank

答えて

0

カップルここに私のリポジトリです。
まず、この動作は再現できません。それはうまく動作します。

第2に、RESTエンドポイントを公開するために別のコントローラー(@RestController)クラスを使用する必要はありません(特定の理由により必要がない限り)。 spring-data-rest(@RepositoryRestResource)を使用すると、springはfindByMenuIdのようなカスタムクエリーメソッドのエンドポイントを含め、エンドポイントを公開します。
例では、コントローラクラスを削除してアプリケーションを再起動するだけです。そしてブラウザからhttp://host:port/mealを打つと、それはリンクとしてすべてのポジションオプションを表示します。これは自明です。

+0

ありがとうございました。それは私にたくさんの助けになる –

関連する問題