JPA
[JPA] 31. RestAPI 주소설계와 세팅
Song hyun
2024. 10. 23. 09:20
728x90
반응형
[JPA] 31. RestAPI 주소설계와 세팅
1. yml - mustache 삭제
mustache:
servlet:
expose-session-attributes: true # Mustache ????? ?? ??? ??? ? ??? ??
expose-request-attributes: true # Mustache ????? ?? ??? ??? ? ??? ??
2. [templates] 폴더 내부의 mustache 파일 모두 삭제
3. 페이지 요청 메서드 삭제
4. WebConfig 수정
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 로그인 인터셉터 등록
registry.addInterceptor(loginInterceptor)
.addPathPatterns("api/**") // 인터셉터를 적용할 경로 패턴 설정
.excludePathPatterns("/board/{id:\\d+}");
// 인터셉터 적용에서 제외할 URL 패턴 설정
// /board/1, /board/33 <-- 로그인 인터셉터에서 제외
// \d+ 숫자 하나 이상을 의미하는 정규 표현식 패턴
// 관리자용 인터셉터 등록
}
5. Restful API 주소설계
(1) 회원 정보 관리
- 회원 가입 -> @PostMapping("/join")
- 로그인 -> @PostMapping("/login")
- 로그아웃 -> @GetMapping("/logout")
- 회원정보 수정 -> @PutMapping("/api/users/{id}")
- 회원정보 수정 페이지 -> @GetMapping ("/api/users/{id}")
(2) 게시글
- 게시글 전체 조회 -> @GetMapping("/"), @GetMapping("/boards")
- 게시글 삭제 -> @DeleteMapping("/api/boards/{id}")
- 게시글 수정 -> @PutMapping("/api/boards/{id}")
- 게시글 등록 -> @PostMapping("/api/boards")
- 게시글 상세 보기 -> @GetMapping("/boards/{id}")
(3) 댓글
- 댓글 삭제 -> @DeleteMapping("/api/replies/{id}")
- 댓글 등록 -> @PostMapping("/api/replies")
*Restful API 주소 설계에 기반한 메서드 수정 예시
/**
* 사용자 정보 수정
* @param updateDTO
* @return 메인 페이지
*/
@PutMapping("/api/users/{id}")
public String update(@RequestBody UserDTO.UpdateDTO updateDTO) {
User sessionUser = (User) session.getAttribute("sessionUser");
if (sessionUser == null) {
return "redirect:/login-form";
}
User updatedUser = userService.updateUser(sessionUser.getId(), updateDTO);
// 세션 정보 동기화 처리
session.setAttribute("sessionUser", updatedUser);
return "redirect:/";
}
728x90
반응형