Я использовал SpringBoot, и в методе PUT я проверяю, существует ли оценка, затем я хочу обновить ее, а также обновить историю, добавив к ней последнюю оценку.
Класс оценки:
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 score;
@NotBlank
@JsonView(Views.class)
private String player;
@NotBlank
@JsonView(Views.class)
private String code;
@JsonView(Views.class)
private Date date;
private List<History> history;
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String getPlayer() {
return player;
}
public void setPlayer(String player) {
this.player = player;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<History> getHistory() {
return history;
}
public void setHistory(List<History> history) {
this.history = history;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "Score [score=" + score + ", player=" + player + ", code=" + code + ", history=" + history + ", date="
+ date + "]";
}
}
Репозиторий Score:
package thesisMongoProject.Repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import thesisMongoProject.Score;
import thesisMongoProject.ScoreDto;
@Repository
public interface ScoreRepository extends MongoRepository<Score, String>{
public Score findByScore(String score);
public void save(ScoreDto scoredto, String score);
}
Но метод PUT сохраняет новый экземпляр в MongoDB вместо обновления существующего
Метод PUT:
//Update Score By ID
@PutMapping("/{score}")
public ResponseEntity<?> updatePlayerByID(
@PathVariable("score")String score,
@RequestBody @JsonView(Views.class) @Valid Score score1){
Score findscore = srepo.findByScore(score);
if(findscore == null)
return ResponseEntity.status(404).body("There is not Score!");
else {
history = new ArrayList<History>();
h = new History();
h.setScore(score1.getScore());
h.setDate(score1.getDate());
history.add(h);
score1.setHistory(history);
srepo.save(score1);
return ResponseEntity.ok(score1);
}
}
Также я пытался использовать ScoreDTO и @PatchMapping вот так:
Класс ScoreDTo:
package thesisMongoProject;
import java.util.List;
public class ScoreDto {
private String score;
List<History> history;
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public List<History> getHistory() {
return history;
}
public void setHistory(List<History> history) {
this.history = history;
}
}
И PATCHMAPPING:
@PatchMapping("/{score}")
public ResponseEntity<?> updateByScore(
@PathVariable("score")String score,
@RequestBody ScoreDto score1){
Score findscore = srepo.findByScore(score);
if(findscore == null)
return ResponseEntity.status(404).body("There is not Score!");
else {
srepo.save(score1, score);
return ResponseEntity.ok(score1);
}
}
но в моей консоли есть ошибка:
org.springframework.data.mapping.PropertyReferenceException: для типа Score не найдено сохранение свойства! Вы имели в виду «дата»?
не могли бы вы мне помочь, как я могу обновить существующее поле оценки, пожалуйста ?!