Skip to content

Commit

Permalink
merge: 패키지 구조 + exception 세팅, 공통 baseResponse 구현 - #4
Browse files Browse the repository at this point in the history
  • Loading branch information
sjk4618 authored Jan 12, 2025
2 parents 4b29263 + a7902aa commit cc29c37
Show file tree
Hide file tree
Showing 28 changed files with 277 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.cakey.common.response;

import com.cakey.ApiCode;
import com.cakey.exception.ErrorCode;
import org.springframework.http.ResponseEntity;

public class ApiResponseUtil {
public static ResponseEntity<BaseResponse<?>> success(final SuccessCode successCode) {
return org.springframework.http.ResponseEntity.status(successCode.getHttpStatus())
.body(BaseResponse.of(successCode));
}

public static <T> ResponseEntity<BaseResponse<?>> success(final SuccessCode successCode, final T data) {
return ResponseEntity.status(successCode.getHttpStatus())
.body(BaseResponse.of(successCode, data));
}

public static ResponseEntity<BaseResponse<?>> failure(final ErrorCode errorCode) {
return ResponseEntity.status(errorCode.getHttpStatus())
.body(BaseResponse.of(errorCode));
}

//따로 error message 넣어줄 때, 사용
public static ResponseEntity<BaseResponse<?>> failure(final ErrorCode errorCode, final String message) {
return ResponseEntity
.status(errorCode.getHttpStatus())
.body(BaseResponse.of(errorCode.getCode(), message));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.cakey.common.response;

import com.cakey.ApiCode;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE)
@AllArgsConstructor(access = lombok.AccessLevel.PRIVATE)
@Builder(access = lombok.AccessLevel.PRIVATE)
public class BaseResponse<T> {
private int code;
private String message;

@JsonInclude(JsonInclude.Include.NON_NULL)
private T data;

public static BaseResponse<?> of(final ApiCode apiMessage) {
return BaseResponse.builder()
.code(apiMessage.getCode())
.message(apiMessage.getMessage())
.build();
}

public static <T> BaseResponse<?> of(SuccessCode successCode, T data) {
return BaseResponse.builder()
.code(successCode.getCode())
.message(successCode.getMessage())
.data(data)
.build();
}

//error 메시지 따로 넣어줄 때, 사용
public static BaseResponse<?> of(final int code, final String message) {
return BaseResponse.builder()
.code(code)
.message(message)
.build();
}
}
40 changes: 40 additions & 0 deletions cakey-api/src/main/java/com/cakey/common/response/SuccessCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.cakey.common.response;

import com.cakey.ApiCode;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public enum SuccessCode implements ApiCode {
/**
* 200 OK
*/
OK(HttpStatus.OK, 20000, "요청이 성공했습니다."),

/**
* 201 Created
*/
CREATED(HttpStatus.CREATED, 200100, "요청이 성공했습니다.");

private final HttpStatus httpStatus;
private final int code;
private final String message;

@Override
public HttpStatus getHttpStatus() {
return httpStatus;
}

@Override
public int getCode() {
return code;
}

@Override
public String getMessage() {
return message;
}
}
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.cakey.exception.handler;

import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
}
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.cakey.user.exception;

import com.cakey.exception.UserBaseException;

public class UserApiException extends UserBaseException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.cakey.user.exception;

import com.cakey.ApiCode;
import org.springframework.http.HttpStatus;

public enum UserErrorCode implements ApiCode {
;

@Override
public HttpStatus getHttpStatus() {
return null;
}

@Override
public int getCode() {
return 0;
}

@Override
public String getMessage() {
return "";
}
}
Empty file.
9 changes: 9 additions & 0 deletions cakey-common/src/main/java/com/cakey/ApiCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cakey;

import org.springframework.http.HttpStatus;

public interface ApiCode {
HttpStatus getHttpStatus();
int getCode();
String getMessage();
}
7 changes: 0 additions & 7 deletions cakey-common/src/main/java/com/cakey/Main.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.cakey.exception;

public class CakeyException extends RuntimeException {

}
73 changes: 73 additions & 0 deletions cakey-common/src/main/java/com/cakey/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.cakey.exception;

import com.cakey.ApiCode;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum ErrorCode implements ApiCode {
/**
* 400 Bad Request
*/
BAD_REQUEST(HttpStatus.BAD_REQUEST, 40000, "잘못된 요청입니다."),
BAD_REQUEST_REQUEST_BODY_VALID(HttpStatus.BAD_REQUEST, 40001, "request body 검증 실패입니다."),
BAD_REQUEST_REQUEST_PARAM_MODELATTRI(HttpStatus.BAD_REQUEST, 40002, "request param 혹은 modelattribute 검증 실패입니다."),
BAD_REQUEST_MISSING_PARAM(HttpStatus.BAD_REQUEST, 40003, "필수 param이 없습니다."),
BAD_REQUEST_METHOD_ARGUMENT_TYPE(HttpStatus.BAD_REQUEST, 40004, "메서드 인자타입이 잘못되었습니다."),
BAD_REQUEST_NOT_READABLE(HttpStatus.BAD_REQUEST, 40005, "json 오류 혹은 reqeust body 필드 오류 입니다."),

/**
* 401 Unauthorized
*/
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, 40100, "리소스 접근 인증 권한이 없습니다."),

/**
* 403 Forbidden
*/
FORBIDDEN(HttpStatus.FORBIDDEN, 40300, "리소스 접근 인가 권한이 없습니다."),

/**
* 404 Not Found
*/
NOT_FOUND_ENTITY(HttpStatus.NOT_FOUND, 40400, "대상을 찾을 수 없습니다."),
NOT_FOUND_API(HttpStatus.NOT_FOUND, 40401, "잘못된 API입니다."),

/**
* 405 Method Not Allowed
*/
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, 40500, "잘못된 HTTP method 요청입니다."),

/**
* 409 Conflict
*/
CONFLICT(HttpStatus.CONFLICT, 40900, "이미 존재하는 리소스입니다."),
INTEGRITY_CONFLICT(HttpStatus.CONFLICT, 40901, "데이터 무결성 위반입니다."),

/**
* 500 Internal Server Error
*/
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, 50000, "서버 내부 오류입니다.");

private final HttpStatus httpStatus;
private final int code;
private final String message;

@Override
public HttpStatus getHttpStatus() {
return httpStatus;
}

@Override
public int getCode() {
return code;
}

@Override
public String getMessage() {
return message;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.cakey.exception;

public class UserBaseException extends CakeyException {

}
3 changes: 3 additions & 0 deletions cakey-domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ dependencies {
implementation project(':cakey-common') // 공통 모듈 의존성
implementation project(':cakey-auth') // 인증 모듈 의존성
implementation project(':cakey-external') // 외부 모듈 의존성

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

}
7 changes: 0 additions & 7 deletions cakey-domain/src/main/java/com/cakey/Main.java

This file was deleted.

19 changes: 19 additions & 0 deletions cakey-domain/src/main/java/com/cakey/common/BaseTimeEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.cakey.common;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Getter
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public abstract class BaseTimeEntity {
@Column(updatable = false)
@CreatedDate
private LocalDateTime createdDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cakey.common;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@Configuration
@EnableJpaAuditing
public class JpaAuditingConfig {
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.cakey.user.exception;

import com.cakey.exception.UserBaseException;

public class UserDomainException extends UserBaseException {
}
Empty file.
Empty file.
7 changes: 0 additions & 7 deletions cakey-external/src/main/java/com/cakey/Main.java

This file was deleted.

Empty file.

0 comments on commit cc29c37

Please sign in to comment.