Java Spring:POST Request在传递JSON对象时输出意外的415错误
我正在创建一个接收以下输入的API,并提供以下输出。
所以我需要有一个猜测function。 到目前为止,我还没有运气测试我的代码。
当我发送POST(使用POSTMan)到http:// localhost:8080 / guess时身体为{"game":"klubxb", "guess":"a"}
。 我将Content-type
设置为application/json
,并且正文为{"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 games = (List) 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; }