게시글 삭제 버튼을 클릭하면 "Request method 'GET' not supported"가 떴다.
@DeleteMapping을 사용하려고 했는데 이런 에러가 떴다.
@GetMapping으로 바꾸면 에러가 사라지지만, @DeleteMapping을 쓰고 싶어서 방법을 찾아봤다.
spring.mvc.hiddenmethod.filter.enabled=true
application.properties에 추가한다.
// 삭제
@DeleteMapping("/post/delete/{id}")
public String delete(@PathVariable("id") Long id) {
postService.deletePost(id);
return "redirect:/";
}
컨트롤러
<div style="float:left;">
<form th:action="@{'/post/delete/'+${post.id}}" th:method="delete">
<button class="btn btn-primary" onclick="deletePost();">삭제</button>
</form>
</div>
th:method="delete"로 한다.
hidden으로 input 태그를 넣는 방법 말고 이런 방법도 있다. 더 간단하고 좋음!
참고 👉
https://pinokio0702.tistory.com/209
Spring Boot에서 HttpMethod delete 사용할 때 주의할 점.
Http Method중 하나인 delete요청을 처리하는 기능을 추가하면서 다음과 같은 에러가 발생했습니다. There was an unexpected error (type=Method Not Allowed, status=405). Request method 'POST' not supported..
pinokio0702.tistory.com
반응형