【发布时间】:2018-02-11 02:02:00
【问题描述】:
我正在使用 MongoDB 阅读和学习 Spring Boot 数据。我的数据库中有大约 10 条记录,格式如下:
{
"_id" : ObjectId("5910c7fed6df5322243c36cd"),
name: "car"
}
当我打开网址时:
http://localhost:8090/items
我得到了所有项目的详尽清单。但是,我想使用MongoRepository 的方法,例如findById、count 等。当我这样使用它们时:
http://localhost:8090/items/count
http://localhost:8090/items/findById/5910c7fed6df5322243c36cd
http://localhost:8090/items/findById?id=5910c7fed6df5322243c36cd
我得到一个 404。
我的设置是这样的:
@SpringBootApplication
public class Application {
public static void main(String[] args) throws IOException {
SpringApplication.run(Application.class, args);
}
}
@Document
public class Item implements Serializable {
private static final long serialVersionUID = -4343106526681673638L;
@Id
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@RepositoryRestResource(collectionResourceRel = "item", path = "items")
public interface ItemRepository<T, ID extends Serializable> extends MongoRepository<Item, String>, ItemRepositoryCustom {
}
我做错了什么?我需要实现MongoRepository 定义的方法还是会自动实现?我迷路了,长期以来一直试图弄清楚这一点。我的控制器中没有任何方法,它是空的。
【问题讨论】:
-
您是否查看过应用程序启动时查看的请求映射?它将向您展示 Spring Data REST 自动创建的所有映射。
-
请原谅错别字。那应该说“在您的应用程序启动时记录”
-
谢谢安迪。 Eclipse 中是否有可以告诉我们所有映射的功能?抱歉,我不确定。
标签: mongodb spring-boot repository spring-data-mongodb mongorepository