Java Spring:使用多个参数发出POST请求

时间:2018-04-27 12:04:16

标签: java spring rest http spring-boot

我试图在Spring中模拟REST调用: guess {game: 'cdaeaa', guess: 'e' } 输出以下内容: {gameId: 'cdaeaa', word: '____', incorrect: 1, status: 'ACTIVE'}

我基本上需要制作一个需要两个参数的函数。它应该返回游戏数据。游戏类如下:

public class Game {

private final String gameId;
private final String word;
private String guessedWord;
private Set<Character> guessedChars;
private GameStatus status;
private int incorrectGuesses;
private static final int MAX_TRIES = 7;}

然而,当我这样打电话时: http://localhost:8080/guess/{asewqd}/{c} (无论我是否将这些东西放在括号中都没关系。) 我收到以下错误:enter image description here

//POST
//make guess
@RequestMapping(value = "/guess/{game}/{guess}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Game makeGuess(@PathVariable String game, @PathVariable String guess, HttpSession session) throws GameDoesNotExistException, InvalidCharacterException{
    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;
}

2 个答案:

答案 0 :(得分:2)

HTTP 405意味着您尝试使用HTTP GET而不是HTTP POST。

答案 1 :(得分:0)

您使用浏览器发出请求。浏览器默认使用GET方法。

使用适合的工具,如卷发或邮递员来发出POST请求。