오늘 한 일

프로젝트

  • 관리자사이트 중 강사용 메뉴구조도 작성 완료
  • 화면 설계 작성 시작

사이드 프로젝트

  • RESTful하게 api 주소 변경
  • git branch 작성 후 master에 merge

Controller에서 RESTful한 URL 사용

  • 어제 계획한대로 url을 RESTful하게 바꾸려고 하니, url 중간에 변수값이 들어가는 경우가 생겼다. 기존 방식대로 get방식의 @RequestParam을 사용해 작성하면 항상 주소 마지막에 변수값이 들어가는 문제가 있다.
  • 검색해보니 @PathVariable이라는 어노테이션을 사용하면 RESTful하게 파라미터 위치를 자유롭게 조정할 수 있었다.
// HomeController
// 홈 전체조회
@GetMapping("/")
public String homeList(Model model) {
	...
	return "homes/homeList";
}
// 개별 홈 조회
@GetMapping("/homes")
public String selectHome (@RequestParam("homeId")Long homeId, Model model) {
	...
	return "homes/selectHome";
}

// UserController
// 회원가입 폼 조회
@GetMapping("/users/new")
public String createUserForm() {
	return "users/createUserForm";
}
// 회원가입
@PostMapping("/users/new")
public String createUser(UserForm form, Model model) {
	...
	return "redirect:/";
}

// PostController
// 게시물 등록 폼 조회
@GetMapping("/posts/{homeId}/new")
public String createPostForm(@PathVariable("homeId")Long homeId, Model model) {
	model.addAttribute("homeId", homeId);
	return "posts/createPostForm";
}

// 게시물 등록
@PostMapping("/posts/{homeId}/new")
public String createPost(@PathVariable("homeId")Long homeId, PostForm form) {
	...	
	return "redirect:/homes?homeId=" + homeId;
}

// 게시물 수정 폼 조회
@GetMapping("/posts/{homeId}/update/{postId}")
public String updatePostForm(@PathVariable("homeId")Long homeId, @PathVariable("postId")Long postId, Model model) {
	...
	return "posts/updatePostForm";
}

// 게시물 수정
@PostMapping("/posts/{homeId}/update/{postId}")
public String updatePost(PostForm form, @PathVariable("homeId")Long homeId, @PathVariable("postId")Long postId) {
	...
	return "redirect:/homes?homeId=" + form.getHomeId();
}

// 게시물 삭제
@PostMapping("/posts/{homeId}/delete/{postId}")
public String deletePost (@PathVariable("homeId")Long homeId, @PathVariable("postId")Long postId) {
	...
	return "redirect:/homes?homeId=" + homeId;
}

HTML에서 thymeleaf로 RESTful URL 맞추기

  • 일단 그냥 내 예상대로 주소를 적어보니 일부 html에서 controler로 가는 주소값이 잘못되었다는 에러가 발생해서 thymeleaf 문법에 맞게 작성해주었다.

parameter type 에러 해결

Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException

<!-- 에러 발생 코드 -->
<td>
	<a th:href="@{/posts/${owner.id}/update/${post.postId}}" th:text="수정"></a>
</td>
<!--수정 : @{}안 텍스트 양 옆에 | 추가. text와 변수를 함께 작성할 때 사용한다고 배웠던 기호인데 url에도 적용된다-->
<td>
	<a th:href="@{|/posts/${owner.id}/update/${post.postId}|}" th:text="수정"></a>
</td>

GET, POST 타입 에러 해결

WARN 4132 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]

  • <a href="" />로 이동한 url은 get방식인데 controller의 delete는 post 방식 사용해서 일어나는 에러이다.
  • controller를 get으로 바꾸거나 html을 form으로 바꾸는 방법 중 선택해야 했다.
  • delete의 경우 안전하지 않은 메소드이므로 post 방식으로 하기로 결정했다.
  • 검색해보니 thymeleaf의 custom을 사용해 html 메소드를 바꾸는 방법도 있는 것 같았지만 당장 적용하기는 복잡해보였다.
  • html form 태그를 이용해 삭제 요청을 보내는 방식으로 해결했다.
  • form으로 http post요청 보내기
<!-- 에러 발생 코드 -->
<a th:href="@{|/posts/${owner.id}/delete/${post.postId}|}" custom:linkMethod="post" th:text="삭제"></a>

<!-- form으로 요청 전송 -->
<form method="POST" th:action="@{|/posts/${owner.id}/delete/${post.postId}|}">
	<input type="hidden" name="postId" id="postId" th:value="${post.postId}" />
	<button type="submit" name="submit">삭제</button>
</form> 
// PostController.java
// 게시물 삭제
@PostMapping("/posts/{homeId}/delete/{postId}")
public String deletePost (@PathVariable("homeId")Long homeId, @PathVariable("postId")Long postId) {
	postService.deletePost(postId);
	return "redirect:/homes?homeId=" + homeId;
}

git branch 생성 및 merge

  • url 바꾸다가 안될 경우를 고려해 별도 branch로 만들어 작업했다.
  • 별도 branch에서 작동 잘 되는 것을 확인하고 github의 pull request, merge 기능을 이용해 master 브랜치에 merge했다.
profile
문서화를 좋아하는 개발자

0개의 댓글