REST API 에 대한 개념은 이전 글에서 정리하였다. 이번 포스팅에서는 spring-boot 로 구현했던 게시판에 REST API 를 적용해보겠다.
[Web Programming] - [RESTful] REST API 활용하기 (1) - 개념
REST API 아키텍쳐를 적용하기 위한 가장 중요한 규칙이 있었다.
1. 자원을 표현하는 URI 는 동사가 아닌 명사 중심이어야 한다.
2. 자원에 대한 행위를 알맞은 HTTP Method 로 표현한다.
이를 이용한 게시글 :
/* 게시글 등록 */
@RequestMapping(value = "/post", method = RequestMethod.POST)
/* 게시글 수정 */
@RequestMapping(value = "/post", method = RequestMethod.PUT)
/* 게시글 삭제 */
@RequestMapping(value = "/post", method = RequestMethod.DELETE)
※ 게시글 수정하는 URI 를 고치면서 문제가 좀 발생했었다. form 태그는 GET, POST 방식만 지원하고 다른 메서드는 지원하지 않아 PUT 사용이 불가능하다.
해결 :
더보기
1. application.properties :
spring.mvc.hiddenmethod.filter.enabled=true
2. form tag :
<form action="/post" method="post">
<input type="hidden" name="_method" value="PUT">
<form/>
template (.html) :
1. 게시글 등록 (POST)
<form action="/post" method="post">
<input type="hidden" name="board_id" th:attr="value=${board_id}"/>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">제목</span>
</div>
<input type="text" class="form-control" name="title" placeholder="제목을 입력하세요">
</div>
<div class="input-group" style="height:200px;">
<textarea class="form-control" name="content" placeholder="내용을 입력하세요"></textarea>
</div>
<br/>
<input type="submit" class="btn btn-primary btn-block" value="등록" />
</form>
2. 게시글 수정 (PUT)
<form action="/post" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="id" th:value="${post.id}"/>
<input type="hidden" name="board_id" th:value="${post.board_id}">
<table class="table table-hover">
<tr>
<th>
<input type="text" class="form-control" name="title" th:value="${post.title}">
</th>
<th th:text="${post.board_id}"></th>
</tr>
<tr>
<td colspan="2">
<input type="text" class="form-control" name="content" th:value="${post.content}">
</td>
</tr>
</table>
<button type="submit" class="btn btn-light">수정</button>
</form>
3. 게시글 삭제 (DELETE)
<form action="/post" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="id" th:value="${post.id}"/>
<input type="hidden" name="board_id" th:value="${post.board_id}"/>
<button type="submit" class="btn btn-light">삭제</button>
</form>
'Server' 카테고리의 다른 글
node.js 시작하기 (codeanywhere / express) (0) | 2021.02.02 |
---|---|
MySQL 서버 타임존 설정 (0) | 2021.01.22 |
[RESTful] REST API 활용하기 (1) - 개념 (0) | 2021.01.18 |
VRF (Virtual Routing Forwarding) 이란 (0) | 2021.01.09 |
[WAS/WES] Apache Tomcat이란? (0) | 2021.01.06 |