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方法,我试图setScoresetDate对历史类的一个对象,然后我救了他们的hrepo这是历史仓库,然后我加入这类型的历史变量List<History>,在那之后我setHistory我的成绩类的srepo,成绩库。但是,当我执行程序时,在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());无法理解我的错误。

高拉夫·迪曼(Gaurav Dhiman):

初始化两者,之后不应该再获得NPE

private List<History> history=new ArrayList<>();
private History h = new History();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Kubernetes 集群 REST API 错误:500 内部服务器错误

使用 REST API 更新客户导致 500 内部服务器错误

从 Django Rest API 默认路由器(创建对象)获取时出现 500(内部服务器错误)

POST 500(内部服务器错误)

POST 500 内部服务器错误

LinkedIn REST API-内部服务器错误

获取 500 内部服务器错误 SpringBoot

$ .ajax中使用“ POST”方法传递数据时,出现500内部服务器错误

使用Rest Assured发出POST请求时出现内部服务器错误

路由api时.htaccess 500内部服务器错误

尝试在Java SpringBoot中使用REST API时收到错误请求

Outlook 365 Mail Rest API:检索.eml附件时收到内部服务器错误

jQuery Ajax Post导致500内部服务器错误

POST 500(内部服务器错误)ajax,mvc

AJAX POST 500内部服务器错误

POST 500(内部服务器错误)-Laravel和Ajax

POST 500 内部服务器错误——PHP

Node JS Nodemailer POST 内部服务器错误 500

主机上的AJAX POST 500内部服务器错误

$ .post jquery中的内部服务器错误500?

POST 请求上的 Http 500 内部服务器错误

Ajax 提交时出现 500 内部服务器错误

Flask API - 500 内部服务器错误状态代码

POST触发服务器api并响应客户端500(内部服务器错误)

500(内部服务器错误)Laravel 删除方法

PayPal REST API返回500服务器错误的信用卡令牌

使用HttpClient处理500(内部服务器错误)

500内部服务器错误AJAX POST请求laravel服务器

在Django中使用AJAX提交时出现错误500(内部服务器错误)