42. REST API 전환 - METHOD 변경 & 주소 변환하기v7

송민경's avatar
Mar 20, 2024
42. REST API 전환 - METHOD 변경 & 주소 변환하기v7

1. UserController 에 주소 변환하기

  • 인증이 필요 없는 주소는 users를 붙일 필요가 없음
  • 인증과 관련된 controller를 따로 만듦
package shop.mtcoding.blog.user; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; @RequiredArgsConstructor @Controller public class UserController { private final UserService userService; private final HttpSession session; // TODO: 회원 정보 조회 API 필요함 @PutMapping("api/users/{id}") public String update(UserRequest.UpdateDTO reqDTO) { User sessionUser = (User) session.getAttribute("sessionUser"); User newSessionUser = userService.update(sessionUser.getId(), reqDTO); session.setAttribute("sessionUser", newSessionUser); return "redirect:/"; } @PostMapping("/join") // 인증이 필요하지 않아 users를 붙이지 않음 public String join(UserRequest.JoinDTO reqDTO) { userService.join(reqDTO); return "redirect:/"; } @PostMapping("/login") public String login(UserRequest.LoginDTO reqDTO) { User sessionUser = userService.login(reqDTO); session.setAttribute("sessionUser", sessionUser); return "redirect:/"; } @GetMapping("/logout") public String logout() { session.invalidate(); return "redirect:/"; } }
notion image
 

2. WebMvcConfig 수정하기

  • api 주소가 붙은 주소는 인증이 필요함
package shop.mtcoding.blog._core.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import shop.mtcoding.blog._core.intercepter.LoginInterceptor; @Configuration // IoC public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()) .addPathPatterns("/api/**") .excludePathPatterns("/api/boards/{id:\\d+}"); } }
notion image
 

3. UserController 변경된 주소

@PutMapping("/api/users/{id}") @GetMapping("/api/users/{id}") @PostMapping("/join") @PostMapping("/login") @GetMapping("/logout")
 

4. BoardController 주소 변환하기

package shop.mtcoding.blog.board; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import shop.mtcoding.blog.user.User; @RequiredArgsConstructor @Controller public class BoardController { private final HttpSession session; private final BoardService boardService; // TODO: 글 목록(model)을 보여줄 API가 필요함 /*@GetMapping("/")*/ // TODO: 글 상세보기 API 필요 /*@GetMapping("/api/boards/{id}/detail")*/ // TODO: 글 조회 API 필요 /*@GetMapping("/api/boards/{id}")*/ @PostMapping("/api/boards") public String save(BoardRequest.SaveDTO reqDTO) { User sessionUser = (User) session.getAttribute("sessionUser"); boardService.save(reqDTO, sessionUser); return "redirect:/"; } // @Transactional @PutMapping("/api/boards/{id}") public String update(@PathVariable Integer id, BoardRequest.UpdateDTO reqDTO) { User sessionUser = (User) session.getAttribute("sessionUser"); boardService.update(id, sessionUser.getId(), reqDTO); return "redirect:/board/" + id; } @DeleteMapping("/api/boards/{id}") public String delete(@PathVariable Integer id) { User sessionUser = (User) session.getAttribute("sessionUser"); boardService.delete(id, sessionUser.getId()); return "redirect:/"; } }
notion image
 

5. BoardController 변경된 주소

@GetMapping("/") @GetMapping("/api/boards/{id}/detail") @GetMapping("/api/boards/{id}") @PostMapping("/api/boards") @PutMapping("/api/boards/{id}") @DeleteMapping("/api/boards/{id}")
 

6. ReplyController 에 주소 변환하기

  • json으로 응답하기에 페이지 id를 몰라도 됨
package shop.mtcoding.blog.reply; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import shop.mtcoding.blog.user.User; @RequiredArgsConstructor @Controller public class ReplyController { private final ReplyService replyService; private final HttpSession session; @DeleteMapping("/api/replies/{id}") public String delete(@PathVariable Integer replyId) { // json으로 응답하기에 페이지 id를 몰라도 됨 User sessionUser = (User) session.getAttribute("sessionUser"); replyService.delete(replyId, sessionUser.getId()); return "redirect:/board/"; } @PostMapping("api/replies") public String save(ReplyRequest.SaveDTO reqDTO) { User sessionUser = (User) session.getAttribute("sessionUser"); replyService.save(reqDTO, sessionUser); return "redirect:/board/" + reqDTO.getBoardId(); } }
notion image
 

7. ReplyController 변경된 주소

@DeleteMapping("/api/replies/{id}") @PostMapping("/api/replies")
Share article

vosw1