spring - 500内部服务器错误; 在springBoot rest api中使用POST方法时
问题描述
我使用 Spring Boot,POST
为我的播放器创建新分数的方法。在POST
方法中,我检查玩家和游戏是否存在,然后创建新分数并将分数及其相关日期添加到我的分数类的历史记录中。每个分数都有包含分数及其日期的历史记录。历史具有历史类的类型列表
历史课:
package thesisMongoProject;
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "history")
public class History {
@Id
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;
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 Score(@NotBlank String score, String player, String code, List<History> history, Date date) {
super();
this.score = score;
this.player = player;
this.code = code;
this.history = history;
this.date = date;
}
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 + "]";
}
}
和 POST 方法:
@RestController
@RequestMapping("/score")
public class ScoreController {
@Autowired
private ScoreRepository srepo;
@Autowired
private PlayerRepository prepo;
@Autowired
private GamesRepository grepo;
@Autowired
private HistoryRepository hrepo;
private List<History> history;
private History h = null;
//Create Score
@PostMapping
public ResponseEntity<?> createScore(@RequestBody @JsonView(Views.class) @Valid Score score) {
//check player exist
Player p = prepo.findByNickname(score.getPlayer());
//check game's cod exist
Games g = grepo.findByCode(score.getCode());
//check score exist
Score s = srepo.findByScore(score.getScore());
// = hrepo.findByScore(score.getScore());
if(s != null)
{
return ResponseEntity.status(409).body("Conflict!!");
}else if((p != null) && (g != null)) {
h.setScore(score.getScore());
h.setDate(score.getDate());
hrepo.save(h);
history.add(h);
//history.add(hrepo.findById(score.getScore()).get());
score.setHistory(history);
srepo.save(score);
return ResponseEntity.status(201).body("Created!");
}
else {
return ResponseEntity.status(400).body("Bad Request!");
}
}
在我的POST
方法中,我尝试了一个 HistorysetScore
类setDate
的对象,然后我将它们保存hrepo
为 history Repository ,然后我将它添加到类型的 history 变量中List<History>
,之后我setHistory
的 score 类使用srepo
Score repository 。但是当我执行我的程序时,在 PostMan 中我有500 Internal Server Error
,在控制台中我有这个错误:
java.lang.NullPointerException: null
at thesisMongoProject.controller.ScoreController.createScore(ScoreController.java:63) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
这正是setScore
对象 h的线,h.setScore(score.getScore());
我无法理解我的错误是什么。
解决方案
初始化两者,之后你不应该得到 NPE
private List<History> history=new ArrayList<>();
private History h = new History();
推荐阅读
- flutter - 从 API Flutter 获取数据
- svelte - 在 Svelte 中使用 CDN 中的 MDBootstrap 文件有什么缺点
- bluetooth-lowenergy - Gatttool 丢失通知
- c - 在 C 程序中使用 ncurses 库时出现 Gcc 错误
- javascript - 从路由器 nextjs 设置活动的 tabpanel mui
- azure - 在 Azure AD 中为用户组指定不同的 MFA 方法
- swift - 自定义 React Native 库中的 CocoaLumberjack 依赖项
- postgresql - 如何在 jsonb postgres 中的数组元素上创建 b-tree 索引
- blockchain - 我可以稳定地获取 Binance API 数据吗?
- rest - “另一个正在进行的操作”的 HTTP 错误代码