Springboot RequestMapping

조회수 452회

중요부분만 넣었습니다.

JpaBoardController.java

...생략...
@RequestMapping(value="/jpa/board/{boardIdx}", method= RequestMethod.PUT)
    public String updateBoard(BoardEntity board) throws Exception{
        jpaBoardService.saveBoard(board, null);
        return "redirect:/jpa/board";
    }

    @RequestMapping(value="/jpa/board/{boardIdx}", method= RequestMethod.DELETE)
    public String deleteBoard(@PathVariable("boardIdx") int boardIdx) throws Exception{
        jpaBoardService.deleteBoard(boardIdx);
        return "redirect:/jpa/board";
    }
    ..생략...

jpaBoardDetail.html

<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>게시글 상세 화면</title>
    <link rel="stylesheet" th:href="@{/css/style.css}"/>
</head>
<body>
    <div class="container">
        <h2>게시글 상세 화면</h2>
        <form id="frm" method="post">
            <table class="board_detail">
                <colgroup>
                    <col width="15%"/>
                    <col width="35%"/>
                    <col width="15%"/>
                    <col width="35%"/>
                </colgroup>
                <caption>게시글 상세내용</caption>
                <tbody>
                    <tr>
                        <th scope="row">글 번호</th>
                        <td th:text="${board.boardIdx }"></td>
                        <th scope="row">조회수</th>
                        <td th:text="${board.hitCnt }"></td>
                    </tr>
                    <tr>
                        <th scope="row">작성자</th>
                        <td th:text="${board.creatorId }"></td>
                        <th scope="row">작성일</th>
                        <td th:text="${board.createdDatetime }"></td>
                    </tr>
                    <tr>
                        <th scope="row">제목</th>
                        <td colspan="3"><input type="text" id="title" name="title" th:value="${board.title }"/></td>
                    </tr>
                    <tr>
                        <td colspan="4" class="view_text">
                            <textarea title="내용" id="contents" name="contents" th:text="${board.contents }"></textarea>
                        </td>
                    </tr>
                </tbody>
            </table>
            <input type="hidden" id="boardIdx" name="boardIdx" th:value="${board.boardIdx}">
            <input type="hidden" id="method" name="_method"/>
        </form>

        <div class="file_list">
            <a th:each="list : ${board.fileList}" th:href="@{/jpa/board/file(idx=${list.idx}, boardIdx=${board.boardIdx})}" th:text="|${list.originalFileName} (${#numbers.formatInteger(list.fileSize/1000, 1, 'DEFAULT')} kb)|"></a>
        </div>

        <a href="#this" id="list" class="btn">목록으로</a>
        <a href="#this" id="edit" class="btn">수정하기</a>
        <a href="#this" id="delete" class="btn">삭제하기</a>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var boardIdx = $("#boardIdx").val();

            $("#list").on("click", function(){
                location.href = "/jpa/board/";
            });

            $("#edit").on("click", function(){
                $("#method").val("put");

                var frm = $("#frm")[0];
                frm.action = "/jpa/board/"+boardIdx;
                frm.submit();
            });

            $("#delete").on("click", function(){
                $("#method").val("delete");

                var frm = $("#frm")[0];
                frm.action = "/jpa/board/"+boardIdx;
                frm.submit();
            });
        });
    </script>
</body>
</html>

Service하고 Repository부분은 에러가 없는거 같아서 안올렸습니다.

에러내용입니다.

org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported

포스트 방식이 지원이 안되는거 같습니다. 위에 update부분에 @RequestMapping 부분에 method={RequestMethod.POST,RequestMethod.PUT} 방식으로 놔봤는데 update는 잘 이루어졌습니다. 하지만 delete부분도 똑같이 method={RequestMethod.POST,RequestMethod.DELETE} 방식으로 놨습니다.

@RequestMapping(value="/jpa/board/{boardIdx}", method={RequestMethod.PUT,RequestMethod.POST})
    public String updateBoard(BoardEntity board) throws Exception{
        jpaBoardService.saveBoard(board, null);
        return "redirect:/jpa/board";
    }

    @RequestMapping(value="/jpa/board/{boardIdx}", method={RequestMethod.DELETE,RequestMethod.POST})
    public String deleteBoard(@PathVariable("boardIdx") int boardIdx) throws Exception{
        jpaBoardService.deleteBoard(boardIdx);
        return "redirect:/jpa/board";
    }

그리고나서 에러입니다. java.lang.IllegalStateException: Ambiguous handler methods mapped for '/jpa/board/12': {public java.lang.String board.board.controller.JpaBoardController.deleteBoard(int) throws java.lang.Exception, public java.lang.String board.board.controller.JpaBoardController.updateBoard(board.board.entity.BoardEntity) throws java.lang.Exception}

그리고 delete부분에만 post추가하면 수정을 누루면 delete처리됩니다. ㅠㅠ 책이랑 똑같이 해봤는데 왜이러는걸까요

  • 단순한 문제같은데요. /jpa/board/{boardIdx} 에 post 를 두개 설정(update, delete)했으니 Ambiguous 오류가 발생하겠지요. 생각을 해보세요. /jpa/board/{boardIdx} 주소로 post 요청이 들어왔다면 update 와 delete 중 어떤 것을 실행해야 할까요? Ambiguous 한겁니다. 정영훈 2019.11.26 21:48

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)