![](/img/trans.png)
[英]@RequestBody not mapping JSON to Java Object - Spring Boot
[英]Java Spring: How to use @RequestBody to POST JSON Object
我正在创建一个API,该API接收以下输入,并提供以下输出。
我已经为“新”创建了一种工作方法:
@RequestMapping(value = "/new", method = RequestMethod.GET)
public StartedGame startGame(HttpSession session){
List<Game> games = getCurrentGames(session);
Game newGame = new Game(wordList);
games.add(newGame);
return new StartedGame(newGame);
}
它返回以下JSON:
{
"gameId": "kvmuyw",
"word": "_______"
}
但是,我需要创建一个进行猜测的函数。 我还没有运气。 我将此作为我的函数标头,但是它似乎不正确...
@RequestMapping(value = "/guess", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public Game makeGuess(@RequestBody String json, HttpSession session)
您可能想要类似
@RequestMapping(value = "/guess", method = RequestMethod.POST,
consumes = "application/json", produces = "application/json")
public Game makeGuess(@RequestBody Guess guess){
// ..
}
@Data // <- this assuming you're using Lombok - add required accessors if not
public class Guess {
String game;
String guess;
}
但是,如果您收到404 Not Found
,则问题不在于方法定义,而在于您发布到错误的URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.