[英]Spring Boot data, MongoDB not returning results
我正在使用MongoDB阅读和学习Spring Boot数据。 我的数据库中大约有10条记录,格式如下:
{
"_id" : ObjectId("5910c7fed6df5322243c36cd"),
name: "car"
}
以下是我的Java / Spring类:
@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 {
}
MongoDB配置:
@Configuration
@EnableMongoRepositories
public class MongoConfiguration extends AbstractMongoConfiguration {
@Override
protected String getDatabaseName() {
return "itemdb";
}
@Override
public Mongo mongo() throws Exception {
return new MongoClient("localhost", 27017);
}
@Override
protected String getMappingBasePackage() {
return "com.learning.domain";
}
}
我已经从春季开始关注文档,但是当我在浏览器中打开链接时:
http://localhost:1337/items
我的数据库中没有任何物品。 我得到的是:
{
"_embedded" : {
"item" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:1337/items"
},
"profile" : {
"href" : "http://localhost:1337/profile/items"
}
},
"page" : {
"size" : 20,
"totalElements" : 0,
"totalPages" : 0,
"number" : 0
}
}
_embedded对象为空。 我是否必须显式创建一个控制器并实现这些方法,或者Spring-Data是否自动做到这一点? 例如,我想获取计数,然后执行以下操作:
http://localhost:1337/items/count
但这给了我404。
没有选择我的MongoConfiguration,所以我在application.properties中添加了以下详细信息:
spring.data.mongodb.database=itemdb
这解决了问题,现在我可以从查询中获取所有项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.