gpt4 book ai didi

Java Spring : How to use @RequestBody to POST JSON Object

转载 作者:行者123 更新时间:2023-12-02 11:19:00 25 4
gpt4 key购买 nike

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

我已经为“new”创建了一个工作方法:

@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。

关于Java Spring : How to use @RequestBody to POST JSON Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50069436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com