1. REST Docs API 문서 작성 가이드

API 문서는 MockMvc 테스트 코드 내의 .andDo(document(...)) 블록을 통해 생성됩니다. 이 블록은 restdocs-api-spec 플러그인이 openapi3.json 파일을 생성하는 데 사용하는 명세서입니다.

1.1. REST Docs + OpenAPI3 구조 개요

구성 요소 역할
Spring REST Docs MockMvc 테스트 시 API 스펙(요청/응답)을 자동으로 스니펫(snippet)으로 생성
restdocs-api-spec 생성된 스니펫을 OpenAPI3(JSON) 형식으로 변환 (build/api-spec/openapi3.json)
springdoc-openapi-ui 해당 JSON을 Swagger UI에서 시각화
Swagger UI 최종 문서 및 API 테스트 UI (운영 서버에서 접근 가능)

1.2. 구조

모든 문서는 document() 함수와 resource() 헬퍼를 기반으로 작성됩니다. 예제는 별도로 ResultAction을 저장하고 있으나 mockMvc.perform에 이어서 쓰셔도 무방합니다.

// when
ResultActions resultActions = mockMvc.perform(
        post("/api/v1/auth/signup")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(request)));

// then
resultActions
        .andExpect(status().isCreated())
        .andExpect(jsonPath("$.data.email").value("[email protected]"))
        .andExpect(jsonPath("$.data.username").value("테스트이름"))
        .andDo(print());

// docs
resultActions.andDo(document(
    "auth-signUp", // 1. 고유 식별자 (도메인-Cotnroller 메서드명)
    preprocessRequest(prettyPrint()),  // (요청/응답 JSON 예쁘게 포맷)
    preprocessResponse(prettyPrint()),
    resource( // 2. OpenAPI 스펙 정의 시작
            ResourceSnippetParameters.builder()
                    .summary("회원가입 API") // 3. 요약 (Swagger의 한 줄 설명, API 이름)
                    .description("...")   // 4. 상세 설명
                    .tag("Auth")          // 5. 그룹 태그 (Swagger의 API 그룹)

                    // --- 스키마 정의 ---
                    .requestSchema(Schema.schema("Auth.SignUpRequest")) // 6. 요청 스키마 이름
                    .responseSchema(Schema.schema("Auth.SignUpResponse"))// 7. 응답 스키마 이름

                    // --- 필드 상세 정의 ---
                    .requestFields( ... ) // 8. 요청 필드
                    .responseFields( ... ) // 9. 응답 필드

                    .build()
    )));

1.3. 항목별 가이드

  1. .tag("Auth")
  1. .requestFields(...).responseFields(...)
  1. .requestSchema(...).responseSchema(...)