Java Spring:POST Request在传递JSON对象时输出意外的415错误

时间:2018-04-28 00:25:51

标签: java json spring rest spring-boot

我正在创建一个接收以下输入的API,并提供以下输出。 enter image description here

所以我需要有一个猜测功能。到目前为止,我还没有运气测试我的代码。

当我将POST(使用POSTMan)发送到http://localhost:8080/guess,身体为{"game":"klubxb", "guess":"a"}时。我将Content-type设置为application/json,正文为raw {"game":"lmzxmn","guess":"c"}

这是回复:

{
    "timestamp": "2018-04-28T00:40:29.141+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "No message available",
    "path": "/guess"
}

我为猜测而定义的功能是:

 @RequestMapping(value = "/guess", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
    public Game makeGuess(@RequestBody Guess gameAndLetter, HttpSession session) throws GameDoesNotExistException, InvalidCharacterException{
        String game = gameAndLetter.getGame();
        String guess = gameAndLetter.getGuess();
        Game g = getGame(game,session);

        String gameId = g.getId();
        if(gameId.equals(game) && guess.length() > 0) {
            boolean correct = compareWords(guess, g);
            if(!correct){
                g.incIncorrect_guesses();
            }
            g.setStatus();
        }
        else{
            if(!gameId.equals(game)) {
                throw new GameDoesNotExistException(game);
            }
            else{
                throw new InvalidCharacterException(guess);
            }
        }
        g = getGame(game,session);

        return g;
    }

我从服务器端收到NullPointerException:

https://pastebin.com/sczmbDri

这是getGame的代码:

// Find an existing game
    private Game getGame(String id, HttpSession session) throws GameDoesNotExistException{
        List<Game> games = (List<Game>) session.getAttribute("games");
        Game g = null;
        for(int i = 0; i < games.size(); i++){
            g = games.get(i);
            if(g.getId().equals(id)){
                break;
            }
        }
        if (g == null) {
            throw new GameDoesNotExistException(id);
        }
        return g;
    }

0 个答案:

没有答案