-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: 패키지 구조 + exception 세팅, 공통 baseResponse 구현 - #4
- Loading branch information
Showing
28 changed files
with
277 additions
and
21 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
cakey-api/src/main/java/com/cakey/common/response/ApiResponseUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
cakey-api/src/main/java/com/cakey/common/response/BaseResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
cakey-api/src/main/java/com/cakey/common/response/SuccessCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
7 changes: 7 additions & 0 deletions
7
cakey-api/src/main/java/com/cakey/exception/handler/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
6 changes: 6 additions & 0 deletions
6
cakey-api/src/main/java/com/cakey/user/exception/UserApiException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
23 changes: 23 additions & 0 deletions
23
cakey-api/src/main/java/com/cakey/user/exception/UserErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
cakey-common/src/main/java/com/cakey/exception/CakeyException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
73
cakey-common/src/main/java/com/cakey/exception/ErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
5 changes: 5 additions & 0 deletions
5
cakey-common/src/main/java/com/cakey/exception/UserBaseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.cakey.exception; | ||
|
||
public class UserBaseException extends CakeyException { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
cakey-domain/src/main/java/com/cakey/common/BaseTimeEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
9 changes: 9 additions & 0 deletions
9
cakey-domain/src/main/java/com/cakey/common/JpaAuditingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
6 changes: 6 additions & 0 deletions
6
cakey-domain/src/main/java/com/cakey/user/exception/UserDomainException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file was deleted.
Oops, something went wrong.
Empty file.