Spring Boot 2.1.xから2.2.0に更新すると、PUTメソッドやDELETEメソッドがRequest method 'POST' not supportedとなる問題の解決

ドハマリしたので、ブログにも書いておきます。

Spring Boot 2.1.xではHTTPのput/deleteメソッドがうまく動いていたアプリをSpring Boot 2.2.0に更新すると動かなくなりました。

簡単なサンプルアプリを以下に置きました。

github.com

例えば、Thymeleafにて下記のコードのようにth:method="put"と指定しておいて...

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <title>編集</title>
</head>
<body>
<div class="container">
    <form th:action="@{/todo/{id}(id=*{todoId})}" th:method="put" th:object="${todo}">
        <div class="form-group">
            <label class="control-label">タイトル</label>
            <input type="text" class="form-control" th:field="*{todoTitle}">
        </div>
        <button class="btn btn-primary" type="submit">更新</button>
    </form>
    <a href="/todo" class="btn btn-primary">一覧画面へ</a>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>

controllerで@PutMappingを指定します。

    @PutMapping("{id}")
    public String update(@PathVariable Integer id, @ModelAttribute Todo todo) {
        todo.setTodoId(id.toString());
        todoRepository.save(todo);
    }

Spring Boot 2.1.xでは動きますが、Spring Boot 2.2.0ではorg.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supportedという例外を吐きます。

色々と検索したのですが答えが出ず、Twitterで聞いてみたら@fukai_yasさんが速攻で回答をくれました。

@fukai_yasさんが示されたStackOverFlowがそのままの答えだった。spring.mvc.hiddenmethod.filter.enabled=trueapplication.propertiesに書けば動きました。

stackoverflow.com

ちゃんとリリースノートにも記されていたorz...

github.com