From 5abe2932bc8a0559bf07e82584460bcebf21fb68 Mon Sep 17 00:00:00 2001 From: sohot8653 Date: Wed, 27 Aug 2025 08:39:23 +0900 Subject: [PATCH] =?UTF-8?q?[API=20=EC=9D=91=EB=8B=B5=20=EA=B0=9C=EC=84=A0]?= =?UTF-8?q?=20ApiResponseDto=EC=97=90=20success=20=ED=95=84=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=EB=A1=9C=20=EC=84=B1=EA=B3=B5/=EC=8B=A4?= =?UTF-8?q?=ED=8C=A8=20=EC=97=AC=EB=B6=80=20=EB=AA=85=ED=99=95=ED=99=94.?= =?UTF-8?q?=20ApiResponseCode=EC=97=90=20COMMON=5FCODE=5FDUPLICATE=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EA=B4=80=EB=A0=A8=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EC=88=98=EC=A0=95.=20CommonCodeServiceImp?= =?UTF-8?q?l=EC=97=90=EC=84=9C=20=EC=A4=91=EB=B3=B5=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EA=B0=9C=EC=84=A0?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 16 +++++++++++++++- .../service/CommonCodeServiceImpl.java | 3 ++- .../global/constants/ApiResponseCode.java | 3 ++- .../bio_backend/global/dto/ApiResponseDto.java | 15 ++++++++++----- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4e8c017..c79c734 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ src/main/java/com/bio/bio_backend/ ```java public class ApiResponseDto { + private boolean success; // 성공/실패 여부 (true/false) private int code; // HTTP 상태 코드 private String message; // 응답 메시지 (ApiResponseCode enum 값) private String description; // 응답 설명 @@ -56,12 +57,23 @@ public class ApiResponseDto { } ``` +#### 상수 정의 + +```java +public class ApiResponseDto { + private static final boolean SUCCESS = true; + private static final boolean FAIL = false; + // ... 필드들 +} +``` + #### 응답 예시 **성공 응답 (201 Created)** ```json { + "success": true, "code": 201, "message": "COMMON_SUCCESS_CREATED", "description": "Created successfully", @@ -77,6 +89,7 @@ public class ApiResponseDto { ```json { + "success": false, "code": 409, "message": "USER_ID_DUPLICATE", "description": "User ID already exists" @@ -131,7 +144,8 @@ public enum ApiResponseCode { - **모든 API 응답**: `ApiResponseDto`로 감싸서 반환 - **공용 응답 코드**: `COMMON_` 접두사로 시작하는 범용 코드 사용 -- **일관된 구조**: `code`, `message`, `description`, `data` 필드로 표준화 +- **일관된 구조**: `success`, `code`, `message`, `description`, `data` 필드로 표준화 +- **성공/실패 구분**: `success` 필드로 명확한 성공/실패 여부 전달 - **제네릭 활용**: ``를 통해 다양한 데이터 타입 지원 ### 3. JWT 인증 시스템 diff --git a/src/main/java/com/bio/bio_backend/domain/admin/common_code/service/CommonCodeServiceImpl.java b/src/main/java/com/bio/bio_backend/domain/admin/common_code/service/CommonCodeServiceImpl.java index 832504c..199b43c 100644 --- a/src/main/java/com/bio/bio_backend/domain/admin/common_code/service/CommonCodeServiceImpl.java +++ b/src/main/java/com/bio/bio_backend/domain/admin/common_code/service/CommonCodeServiceImpl.java @@ -8,6 +8,7 @@ import com.bio.bio_backend.domain.admin.common_code.mapper.CommonCodeMapper; import com.bio.bio_backend.domain.admin.common_code.mapper.CommonGroupCodeMapper; import com.bio.bio_backend.domain.admin.common_code.repository.CommonCodeRepository; import com.bio.bio_backend.domain.admin.common_code.repository.CommonGroupCodeRepository; +import com.bio.bio_backend.global.constants.AppConstants; import com.bio.bio_backend.global.exception.ApiException; import com.bio.bio_backend.global.constants.ApiResponseCode; import lombok.RequiredArgsConstructor; @@ -33,7 +34,7 @@ public class CommonCodeServiceImpl implements CommonCodeService { @Transactional public CommonGroupCodeDto createGroupCode(CommonGroupCodeDto groupCodeDto) { if (commonGroupCodeRepository.existsByCode(groupCodeDto.getCode())) { - throw new ApiException(ApiResponseCode.USER_ID_DUPLICATE, "이미 존재하는 그룹 코드입니다: " + groupCodeDto.getCode()); + throw new ApiException(ApiResponseCode.COMMON_CODE_DUPLICATE); } CommonGroupCode groupCode = commonGroupCodeMapper.toCommonGroupCode(groupCodeDto); diff --git a/src/main/java/com/bio/bio_backend/global/constants/ApiResponseCode.java b/src/main/java/com/bio/bio_backend/global/constants/ApiResponseCode.java index 54f588d..fc5bb88 100644 --- a/src/main/java/com/bio/bio_backend/global/constants/ApiResponseCode.java +++ b/src/main/java/com/bio/bio_backend/global/constants/ApiResponseCode.java @@ -14,7 +14,7 @@ public enum ApiResponseCode { /*공통 Code*/ // 200 OK - COMMON_SUCCESS(HttpStatus.OK.value(), "요청 성공"), + COMMON_SUCCESS(HttpStatus.OK.value(), "요청을 성공하였습니다"), COMMON_SUCCESS_CREATED(HttpStatus.CREATED.value(), "성공적으로 생성되었습니다"), COMMON_SUCCESS_UPDATED(HttpStatus.OK.value(), "성공적으로 수정되었습니다"), COMMON_SUCCESS_DELETED(HttpStatus.OK.value(), "성공적으로 삭제되었습니다"), @@ -39,6 +39,7 @@ public enum ApiResponseCode { // 409 Conflict COMMON_CONFLICT(HttpStatus.CONFLICT.value(), "충돌이 발생했습니다"), + COMMON_CODE_DUPLICATE(HttpStatus.CONFLICT.value(), "동일한 코드가 존재합니다"), // 500 Internal Server Error COMMON_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR.value(), "서버에서 오류가 발생했습니다"), diff --git a/src/main/java/com/bio/bio_backend/global/dto/ApiResponseDto.java b/src/main/java/com/bio/bio_backend/global/dto/ApiResponseDto.java index 105f20c..8f1709f 100644 --- a/src/main/java/com/bio/bio_backend/global/dto/ApiResponseDto.java +++ b/src/main/java/com/bio/bio_backend/global/dto/ApiResponseDto.java @@ -12,12 +12,17 @@ import lombok.RequiredArgsConstructor; @JsonInclude(JsonInclude.Include.NON_NULL) public class ApiResponseDto { + private static final boolean SUCCESS = true; + private static final boolean FAIL = false; + + private boolean success; private int code; private String message; private String description; private T data; - private ApiResponseDto(int code, String message, String description, T data){ + private ApiResponseDto(boolean success, int code, String message, String description, T data){ + this.success = success; this.code = code; this.message = message; this.description = description; @@ -25,19 +30,19 @@ public class ApiResponseDto { } public static ApiResponseDto success(ApiResponseCode responseCode, T data) { - return new ApiResponseDto(responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), data); + return new ApiResponseDto(SUCCESS, responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), data); } public static ApiResponseDto success(ApiResponseCode responseCode) { - return new ApiResponseDto(responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), null); + return new ApiResponseDto(SUCCESS, responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), null); } public static ApiResponseDto fail(ApiResponseCode responseCode, T data) { - return new ApiResponseDto(responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), data); + return new ApiResponseDto(FAIL, responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), data); } public static ApiResponseDto fail(ApiResponseCode responseCode) { - return new ApiResponseDto(responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), null); + return new ApiResponseDto(FAIL, responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), null); } }