[英]Why isn't REST accessing existing entries in MongoDB?
I have an example entry in MongoDB.我在 MongoDB 中有一个示例条目。 I am using Spring following the tutorial found here: https://spring.io/guides/gs/accessing-mongodb-data-rest/我按照此处找到的教程使用 Spring: https : //spring.io/guides/gs/accessing-mongodb-data-rest/
Everything loads fine, but for whatever reason it's not recognizing that entry in the database.一切正常,但无论出于何种原因,它都无法识别数据库中的该条目。
Player.class播放器类
package com.company.botmiteserver;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
public class Player {
@Id
public ObjectId _id;
public String email;
public String account_name;
public String password;
public String pin;
public String status;
public String type;
public boolean active;
public Player () { }
public Player(ObjectId _id, String email, String account_name, String password, String pin, String status, String type, Boolean active) {
this._id = _id;
this.email = email;
this.account_name = account_name;
this.password = password;
this.pin = pin;
this.status = status;
this.type = type;
this.active = active;
}
public String get_id() { return _id.toHexString(); }
public void set_id(ObjectId _id) { this._id = _id; }
public String getEmail() {
return email;
}
public String getAccountName() {
return account_name;
}
public String getPassword() {
return password;
}
public String getPin() {
return pin;
}
public String getStatus() {
return status;
}
public String getType() {
return type;
}
public String setType(String type) {
return this.type = type;
}
public Boolean getActive() {
return active;
}
public Boolean setActive(Boolean active) {
return this.active = active;
}
} }
PlayerRepository.class PlayerRepository.class
package com.company.botmiteserver;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import java.util.List;
@RepositoryRestResource(collectionResourceRel = "player", path = "player")
public interface PlayerRepository extends MongoRepository<Player, String> {
Player findBy_id(ObjectId _id);
// public Player findByEmail(String email);
// public List<Player> findByType(@Param("type") String account_name);
}
PlayerController.class PlayerController.class
package com.company.botmiteserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/player")
public class PlayerController {
@Autowired
private PlayerRepository repository;
}
Result:结果:
{
"_embedded" : {
"player" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/player"
},
"profile" : {
"href" : "http://localhost:8080/profile/player"
},
"search" : {
"href" : "http://localhost:8080/player/search"
}
},
"page" : {
"size" : 20,
"totalElements" : 0,
"totalPages" : 0,
"number" : 0
}
}
It was so obvious.太明显了。 I needed to configure the spring.data.mongodb.database
in the application.properties我需要在spring.data.mongodb.database
中配置spring.data.mongodb.database
ie spring.data.mongodb.database = foo
即spring.data.mongodb.database = foo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.