我将Spring Boot rest api与MongoDb一起使用。 我有四个班级,玩家班级,游戏班级,历史班级和得分班。 我将嵌入式文档与Spring Data和MongoDB一起使用,在我的项目中,我想在POST方法中为现有的Player和GameCode创建新的分数。 但是我有此错误,无法理解为什么?
得分类别:
package thesisMongoProject;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.NotBlank;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import com.fasterxml.jackson.annotation.JsonView;
@Document(collection = "score")
public class Score {
@Id
@NotBlank
@JsonView(Views.class)
private String scoreID;
@NotBlank
@JsonView(Views.class)
private String score;
@NotBlank
@JsonView(Views.class)
private Player player;
@NotBlank
@JsonView(Views.class)
private Games gamecode;
@JsonView(Views.class)
private Date date;
private List<History> history;
public Score(@NotBlank String scoreID, @NotBlank String score, @NotBlank Player player, @NotBlank Games gamecode,
Date date, List<History> history) {
super();
this.scoreID = scoreID;
this.score = score;
this.player = player;
this.gamecode = gamecode;
this.date = date;
this.history = history;
}
public String getScoreID() {
return scoreID;
}
public void setScoreID(String scoreID) {
this.scoreID = scoreID;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
public Games getGamecode() {
return gamecode;
}
public void setGamecode(Games gamecode) {
this.gamecode = gamecode;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public List<History> getHistory() {
return history;
}
public void setHistory(List<History> history) {
this.history = history;
}
}
玩家职业:
package thesisMongoProject;
import javax.validation.constraints.NotBlank;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "player")
public class Player {
@Id
@NotBlank
private String nickname;
@NotBlank
private String firstname;
@NotBlank
private String lastname;
@NotBlank
private String email;
public Player(String nickname, String firstname, String lastname, String email) {
super();
this.nickname = nickname;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Player [nickname=" + nickname + ", firstname=" + firstname + ", lastname=" + lastname + ", email="
+ email + "]";
}
}
游戏类:
package thesisMongoProject;
import javax.validation.constraints.NotBlank;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "game")
public class Games {
@Id
@NotBlank
private String code;
@NotBlank
private String title;
@NotBlank
private String software_house;
@NotBlank
private String version;
@NotBlank
private String release_year;
public Games(@NotBlank String code, @NotBlank String title, @NotBlank String software_house,
@NotBlank String version, @NotBlank @NotBlank @NotBlank String release_year) {
super();
this.code = code;
this.title = title;
this.software_house = software_house;
this.version = version;
this.release_year = release_year;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSoftware_house() {
return software_house;
}
public void setSoftware_house(String software_house) {
this.software_house = software_house;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public @NotBlank String getRelease_year() {
return release_year;
}
public void setRelease_year(@NotBlank String release_year) {
this.release_year = release_year;
}
@Override
public String toString() {
return "Games [code=" + code + ", title=" + title + ", software_house=" + software_house + ", version="
+ version + ", release_year=" + release_year + "]";
}
}
历史课:
package thesisMongoProject;
import java.util.Date;
public class History {
private String score;
private Date date;
public History(String score, Date date) {
super();
this.score = score;
this.date = date;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "History [score=" + score + ", date=" + date + "]";
}
}
视图类:
package thesisMongoProject;
public class Views {
public static class Creat{}
}
分数存储库:
package thesisMongoProject.Repository;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;
import thesisMongoProject.Games;
import thesisMongoProject.Player;
import thesisMongoProject.Score;
@Repository
public interface ScoreRepository extends MongoRepository<Score, String>{
public List<Score> findByScore(String score);
@Query("{'player.nickname':?0}")
public List<Score> findByPlayerName(Player player);
@Query("{'gamecode.code':?0}")
public List<Score> findByGameCode(Games game);
}
和POST方法:
@RestController
@RequestMapping("/score")
public class ScoreController {
@Autowired
private ScoreRepository srepo;
@Autowired
//private List<History> history;
//Create Score
@PostMapping
public ResponseEntity<?> createScore(@RequestBody @JsonView(Views.class) Score score) {
//String player = score.getNickname();
//String code = score.getCode();
List<Score> p = srepo.findByPlayerName(score.getPlayer());
List<Score> g = srepo.findByGameCode(score.getGamecode());
if ((p.isEmpty())&&(g.isEmpty()))
return ResponseEntity.status(400).body("Bad Request!");
else {
srepo.save(score);
return ResponseEntity.ok(score);
}
}
我的控制台中的错误是:
Description:
Parameter 0 of method createScore in thesisMongoProject.controller.ScoreController required a bean of type 'thesisMongoProject.Score' that could not be found.
The injection point has the following annotations:
- @org.springframework.web.bind.annotation.RequestBody(required=true)
- @com.fasterxml.jackson.annotation.JsonView({thesisMongoProject.Views.class})
Action:
Consider defining a bean of type 'thesisMongoProject.Score' in your configuration.
能帮我个忙吗,请
答案 0 :(得分:0)
问题是,在定义得分存储库实例之后,在得分控制器中,
{
"query": {
"bool" : {
"must" : {
"match":{
"customer_name":{
"query":"",
"operator":"and"
}
}
},
"should" : [
{"match":{
"customer_name":{
"query":"",
"operator":"and"
}
}},
{"match":{
"customer_name":{
"query":"",
"operator":"and"
}
}},
],
"minimum_should_match" : 1,
}
}
}
我写了@Autowired注释而不使用,所以当我删除不使用@Autowired注释时,一切运行良好。