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); } }