ドハマリしたので、ブログにも書いておきます。
Spring Boot 2.1.xではHTTPのput/deleteメソッドがうまく動いていたアプリをSpring Boot 2.2.0に更新すると動かなくなりました。
簡単なサンプルアプリを以下に置きました。
例えば、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さんが速攻で回答をくれました。
これとか? https://t.co/YTLg77mwC0
— ふかい やすゆき (@fukai_yas) November 11, 2019
@fukai_yasさんが示されたStackOverFlowがそのままの答えだった。spring.mvc.hiddenmethod.filter.enabled=true
をapplication.properties
に書けば動きました。
ちゃんとリリースノートにも記されていたorz...