RestAPI를 공부한 후 프로젝트에 적용해보고자하는 데 시작하자마자 벽을 만났다..
상황 : RestAPI를 적용한 게시판 중 게시판 '수정' 메소드가 실행이 안 되는 상황
@PutMapping("/{no}")
public String updateBoard(@PathVariable("no") Integer boardId,@ModelAttribute BoardForm boardForm){
boardService.updateBoard(boardForm,boardId);
return "redirect:" +boardId;
}
<form th:action="@{'/boards/'+ ${board.boardId}}" method ="put">
<fieldset>
<ul>
<li><label>제목<input type="text" id="title" name = "title" th:placeholder="${board.title}"></label></li>
<li><label>작성자<input type="text" id="writer" name="writer" th:value="${member.memberName}" readonly></label></li>
<li><label>내용<textarea id="content" name="content" rows="4" cols="50" th:placeholder="${board.content}"></textarea></label></li>
<li><label>작성일<input type='datetime-local' name='localDateTime'/></label></li>
<li><label>공개범위<input type='radio' name='range' value='ALL' checked="checked" />모두에게 공개<input type='radio' name='range' value='ONLY_UNNORMAL' />장애학우들에게만 공개</label></li>
</ul>
<button type="submit">글 수정</button>
</fieldset>
</form>
글 수정 누른 후
URL: http://localhost:8080/boards/1?title=%40postMapping%EC%9D%B4+%EB%AC%B8%EC%A0%9C%EC%9D%B8%EA%B0%80&writer=%ED%99%A9%EB%8C%80%EC%84%A0&content=%EC%96%B4%EB%96%A4+%EA%B2%83%EC%9D%B4+%EB%AC%B8%EC%A0%9C%EC%9D%B8%EA%B0%80&localDateTime=2022-10-30T20%3A14&range=ALL
문제 분석 :
1. boardService의 updateBoard() 메소드가 실행이 되지 않았다.
2. boadService에 문제가 있는 것은 아니다.
3. @PutMapping을 사용하지 않고, @PostMapping을 사용하면, 제대로 작동한다.
4. updateBoard() 메소드에서 리턴값은 실행이 된다. 왜냐하면, url이 http://localhost:8080/boards/1?뭐시기이다.
=> @PutMapping에 무엇인가 문제가 있다.
문제 해결 :
@PutMapping과 @DeleteMapping을 사용하는 방법
1. hiddenHttpMethodFilter() 메소드 추가
@SpringBootApplication
public class SpringbokApplication {
public static void main(String[] args)
{
SpringApplication.run(SpringbokApplication.class, args);
}
@Bean
public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
return new HiddenHttpMethodFilter();
}
}
@PostMapping과 @DeleteMapping을 사용하기위해 @Bean 추가
2. <input type="hidden" name="_method" value="put"> 추가
<form th:action="@{'/boards/'+ ${board.boardId}}" method ="post">
<input type="hidden" name="_method" value="put"/>
<fieldset>
<ul>
<li><label>제목<input type="text" id="title" name = "title" th:placeholder="${board.title}"></label></li>
<li><label>작성자<input type="text" id="writer" name="writer" th:value="${member.memberName}" readonly></label></li>
<li><label>내용<textarea id="content" name="content" rows="4" cols="50" th:placeholder="${board.content}"></textarea></label></li>
<li><label>작성일<input type='datetime-local' name='localDateTime'/></label></li>
<li><label>공개범위<input type='radio' name='range' value='ALL' checked="checked" />모두에게 공개<input type='radio' name='range' value='ONLY_UNNORMAL' />장애학우들에게만 공개</label></li>
</ul>
<button type="submit">글 수정</button>
</fieldset>
</form>
수정 완료 ~!!
감사합니다!!
'스터디 > Spring' 카테고리의 다른 글
[김영한님 스프링 핵심 원리] 빈의 생명 주기와 콜백 (0) | 2022.11.02 |
---|---|
[김영한님 - 스프링 핵심 원리] 생성자 주입을 사용해야하는 이유 (0) | 2022.11.01 |
Postman사용 방법 (0) | 2022.10.10 |
Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'localDateTime' 에러 해결 (0) | 2022.10.07 |
[김영한님 - 스프링의 핵심원리] 컴포넌트 스캔 (0) | 2022.10.02 |