[게시판 프로젝트] API 구현(1) - Spring DATA REST를 통해
2024. 8. 16. 23:50ㆍCS/프로젝트
Spring DATA REST를 이용하면,
우리가 가진 엔티티 클래스와 레포지토리를 통해
바로 빠르게 Restful한 API를 만들 수 있습니다.
기본 기능은 Spring Data Rest를 통하여 구현하고,
복잡한 세부기능은 직접 개발해 보도록 합시다.
API 구현을 위한 build-gradle 의존성 추가
// API 구현 dependencies 추가
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.data:spring-data-rest-hal-explorer'
//우리가 만든 API를 시각적으로 보면서 간단히 테스트 할 수 있도록
application.yml 추가
data:
rest:
base-path: /api
detection-strategy: annotated
HAL explorer를 통해서, API가 잘 구현되었는지 확인
articles에 Get방식으로 HTTP Request를 보내면, 20개를 하나의 페이징 단위로 하여,
JSON 데이터가 내려진 것을 확인할 수 있습니다.
ArticleControllerTest
ArticleControllerTest lombok 및 기본 설정
@Disabled("Spring Data REST 통합테스트는 불필요하므로 제외시킴")
@DisplayName("Data REST - API 테스트")
@Transactional
@AutoConfigureMockMvc
@SpringBootTest
//@WebMvcTest -> Controller에 대한 Test이니까
public class DataRestTest {
private final MockMvc mvc;
public DataRestTest(@Autowired MockMvc mvc) {
this.mvc = mvc;
}
[api] 게시글 리스트 조회
@DisplayName("[api] 게시글 리스트 조회")
@Test
public void givenNothing_whenRequestingArticles_thenReturnsArticlesJsonResponse() throws Exception {
//given
//when & Then
mvc.perform(MockMvcRequestBuilders.get("/api/articles"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.valueOf("application/hal+json")));
/*
Test 오류 뜨는 이유
WebMvc는 slice test -> Controller 외에 불필요하다고 여겨지는 Bean들은 로드하지 않음
Controller와 연관된 최소한을 로드함 = DataRest의 AutoConfiguration을 넣지 않은 것
가장 쉬운 해결책은 IntegrationTest로 작성하는 것 @SpringBootTest
따라서 DB에 영향을 주기 때문에 @Transactional을 추가
/*
}
[api] 게시글 단건 조회
@DisplayName("[api] 게시글 단건 조회")
@Test
public void givenNothing_whenRequestingArticle_thenReturnsArticlesJsonResponse() throws Exception {
//given
//when & Then
mvc.perform(MockMvcRequestBuilders.get("/api/articles/1"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.valueOf("application/hal+json")));
}
[api] 게시글 댓글 조회
@DisplayName("[api] 게시글 댓글 조회")
@Test
public void givenNothing_whenRequestingArticleCommentsFromArticle_thenReturnsArticlesJsonResponse() throws Exception {
//given
//when & Then
mvc.perform(MockMvcRequestBuilders.get("/api/articles/1/articleComments"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.valueOf("application/hal+json")));
}
[api] 게시글 댓글리스트 조회
@DisplayName("[api] 게시글 댓글리스트 조회")
@Test
public void givenNothing_whenRequestingArticleComments_thenReturnsArticlesJsonResponse() throws Exception {
//given
//when & Then
mvc.perform(MockMvcRequestBuilders.get("/api//articleComments"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.valueOf("application/hal+json")));
}
[api] 게시글 댓글 단건 조회
@DisplayName("[api] 댓글 단건 조회")
@Test
public void givenNothing_whenRequestingArticleComment_thenReturnsArticlesJsonResponse() throws Exception {
//given
//when & Then
mvc.perform(MockMvcRequestBuilders.get("/api/articleComments/1"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.valueOf("application/hal+json")));
}
'CS > 프로젝트' 카테고리의 다른 글
[게시판 프로젝트] 뷰 구현(2) - Spring Security를 사용하여 로그인 페이지 구현 (0) | 2024.08.17 |
---|---|
[게시판 프로젝트] 뷰 구현(1) - 타임리프를 사용하여 게시판/게시글 페이지 구현 (0) | 2024.08.17 |
[게시판 프로젝트] API 구현(2) - Querydsl을 통해서 세부 검색 기능 구현 (0) | 2024.08.16 |
[게시판 프로젝트] repository, JPAConfig 생성 (0) | 2024.08.15 |
[게시판 프로젝트] IntelliJ spring boot DB 접근 설정하기 - MySQL (0) | 2024.08.15 |