[API 응답 개선] ApiResponseDto에 success 필드 추가로 성공/실패 여부 명확화. ApiResponseCode에 COMMON_CODE_DUPLICATE 추가 및 관련 메시지 수정. CommonCodeServiceImpl에서 중복 코드 예외 처리 개선.

This commit is contained in:
2025-08-27 08:39:23 +09:00
parent 9467a1edd0
commit 5abe2932bc
4 changed files with 29 additions and 8 deletions

View File

@@ -49,6 +49,7 @@ src/main/java/com/bio/bio_backend/
```java ```java
public class ApiResponseDto<T> { public class ApiResponseDto<T> {
private boolean success; // 성공/실패 여부 (true/false)
private int code; // HTTP 상태 코드 private int code; // HTTP 상태 코드
private String message; // 응답 메시지 (ApiResponseCode enum 값) private String message; // 응답 메시지 (ApiResponseCode enum 값)
private String description; // 응답 설명 private String description; // 응답 설명
@@ -56,12 +57,23 @@ public class ApiResponseDto<T> {
} }
``` ```
#### 상수 정의
```java
public class ApiResponseDto<T> {
private static final boolean SUCCESS = true;
private static final boolean FAIL = false;
// ... 필드들
}
```
#### 응답 예시 #### 응답 예시
**성공 응답 (201 Created)** **성공 응답 (201 Created)**
```json ```json
{ {
"success": true,
"code": 201, "code": 201,
"message": "COMMON_SUCCESS_CREATED", "message": "COMMON_SUCCESS_CREATED",
"description": "Created successfully", "description": "Created successfully",
@@ -77,6 +89,7 @@ public class ApiResponseDto<T> {
```json ```json
{ {
"success": false,
"code": 409, "code": 409,
"message": "USER_ID_DUPLICATE", "message": "USER_ID_DUPLICATE",
"description": "User ID already exists" "description": "User ID already exists"
@@ -131,7 +144,8 @@ public enum ApiResponseCode {
- **모든 API 응답**: `ApiResponseDto<T>`로 감싸서 반환 - **모든 API 응답**: `ApiResponseDto<T>`로 감싸서 반환
- **공용 응답 코드**: `COMMON_` 접두사로 시작하는 범용 코드 사용 - **공용 응답 코드**: `COMMON_` 접두사로 시작하는 범용 코드 사용
- **일관된 구조**: `code`, `message`, `description`, `data` 필드로 표준화 - **일관된 구조**: `success`, `code`, `message`, `description`, `data` 필드로 표준화
- **성공/실패 구분**: `success` 필드로 명확한 성공/실패 여부 전달
- **제네릭 활용**: `<T>`를 통해 다양한 데이터 타입 지원 - **제네릭 활용**: `<T>`를 통해 다양한 데이터 타입 지원
### 3. JWT 인증 시스템 ### 3. JWT 인증 시스템

View File

@@ -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.mapper.CommonGroupCodeMapper;
import com.bio.bio_backend.domain.admin.common_code.repository.CommonCodeRepository; 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.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.exception.ApiException;
import com.bio.bio_backend.global.constants.ApiResponseCode; import com.bio.bio_backend.global.constants.ApiResponseCode;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -33,7 +34,7 @@ public class CommonCodeServiceImpl implements CommonCodeService {
@Transactional @Transactional
public CommonGroupCodeDto createGroupCode(CommonGroupCodeDto groupCodeDto) { public CommonGroupCodeDto createGroupCode(CommonGroupCodeDto groupCodeDto) {
if (commonGroupCodeRepository.existsByCode(groupCodeDto.getCode())) { 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); CommonGroupCode groupCode = commonGroupCodeMapper.toCommonGroupCode(groupCodeDto);

View File

@@ -14,7 +14,7 @@ public enum ApiResponseCode {
/*공통 Code*/ /*공통 Code*/
// 200 OK // 200 OK
COMMON_SUCCESS(HttpStatus.OK.value(), "요청 성공"), COMMON_SUCCESS(HttpStatus.OK.value(), "요청 성공하였습니다"),
COMMON_SUCCESS_CREATED(HttpStatus.CREATED.value(), "성공적으로 생성되었습니다"), COMMON_SUCCESS_CREATED(HttpStatus.CREATED.value(), "성공적으로 생성되었습니다"),
COMMON_SUCCESS_UPDATED(HttpStatus.OK.value(), "성공적으로 수정되었습니다"), COMMON_SUCCESS_UPDATED(HttpStatus.OK.value(), "성공적으로 수정되었습니다"),
COMMON_SUCCESS_DELETED(HttpStatus.OK.value(), "성공적으로 삭제되었습니다"), COMMON_SUCCESS_DELETED(HttpStatus.OK.value(), "성공적으로 삭제되었습니다"),
@@ -39,6 +39,7 @@ public enum ApiResponseCode {
// 409 Conflict // 409 Conflict
COMMON_CONFLICT(HttpStatus.CONFLICT.value(), "충돌이 발생했습니다"), COMMON_CONFLICT(HttpStatus.CONFLICT.value(), "충돌이 발생했습니다"),
COMMON_CODE_DUPLICATE(HttpStatus.CONFLICT.value(), "동일한 코드가 존재합니다"),
// 500 Internal Server Error // 500 Internal Server Error
COMMON_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR.value(), "서버에서 오류가 발생했습니다"), COMMON_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR.value(), "서버에서 오류가 발생했습니다"),

View File

@@ -12,12 +12,17 @@ import lombok.RequiredArgsConstructor;
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResponseDto<T> { public class ApiResponseDto<T> {
private static final boolean SUCCESS = true;
private static final boolean FAIL = false;
private boolean success;
private int code; private int code;
private String message; private String message;
private String description; private String description;
private T data; 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.code = code;
this.message = message; this.message = message;
this.description = description; this.description = description;
@@ -25,19 +30,19 @@ public class ApiResponseDto<T> {
} }
public static <T> ApiResponseDto<T> success(ApiResponseCode responseCode, T data) { public static <T> ApiResponseDto<T> success(ApiResponseCode responseCode, T data) {
return new ApiResponseDto<T>(responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), data); return new ApiResponseDto<T>(SUCCESS, responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), data);
} }
public static <T> ApiResponseDto<T> success(ApiResponseCode responseCode) { public static <T> ApiResponseDto<T> success(ApiResponseCode responseCode) {
return new ApiResponseDto<T>(responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), null); return new ApiResponseDto<T>(SUCCESS, responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), null);
} }
public static <T> ApiResponseDto<T> fail(ApiResponseCode responseCode, T data) { public static <T> ApiResponseDto<T> fail(ApiResponseCode responseCode, T data) {
return new ApiResponseDto<T>(responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), data); return new ApiResponseDto<T>(FAIL, responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), data);
} }
public static <T> ApiResponseDto<T> fail(ApiResponseCode responseCode) { public static <T> ApiResponseDto<T> fail(ApiResponseCode responseCode) {
return new ApiResponseDto<T>(responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), null); return new ApiResponseDto<T>(FAIL, responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), null);
} }
} }