我是Spring Framework的新手。我正在尝试使用Spring Rest和MongoDB创建一个小型REST API。 当我创建存储库以从MongoDb获取数据时,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 :(得分:0)
其次,您不需要单独的控制器(@RestController)类来公开您的REST端点(除非出于特定原因需要)。当您使用spring-data-rest(@RepositoryRestResource)时,spring会为您公开端点,包括自定义查询方法的端点,例如findByMenuId。 在您的示例中,只需删除控制器类并重新启动应用程序。并尝试从浏览器中点击 http://host:port/meal ,它会将所有可能的选项显示为链接,这是不言自明的。