-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
โก Feat : Define common api response #9
Changes from all commits
58973d8
834d9e7
c468749
c4046aa
5e277bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package slvtwn.khu.toyouserver.common; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@JsonPropertyOrder({"code", "message", "data", "pageInfo"}) | ||
public class ApiResponse<T> { | ||
|
||
private final String code; | ||
|
||
private final String message; | ||
|
||
@JsonInclude(Include.NON_NULL) | ||
private final T data; | ||
|
||
private final PageInfoResponse pageInfo; | ||
|
||
ApiResponse(String code, String message) { | ||
this(code, message, null, null); | ||
} | ||
|
||
ApiResponse(String code, String message, T data) { | ||
this(code, message, data, null); | ||
} | ||
|
||
ApiResponse(String code, String message, T data, PageInfoResponse pageInfo) { | ||
this.code = code; | ||
this.message = message; | ||
this.data = data; | ||
this.pageInfo = pageInfo; | ||
} | ||
|
||
public static <T> ApiResponse<T> success(SuccessType successType) { | ||
return new ApiResponse<>(successType.getCode(), successType.getMessage()); | ||
} | ||
|
||
public static <T> ApiResponse<T> success(SuccessType successType, T data) { | ||
return new ApiResponse<>(successType.getCode(), successType.getMessage(), data); | ||
} | ||
|
||
public static <T> ApiResponse<T> success(SuccessType successType, T data, PageInfoResponse pageInfo) { | ||
return new ApiResponse<>(successType.getCode(), successType.getMessage(), data, pageInfo); | ||
} | ||
|
||
public static ApiResponse<?> error(ErrorType errorType) { | ||
return new ApiResponse<>(errorType.getCode(), errorType.getMessage()); | ||
} | ||
|
||
public static <T> ApiResponse<T> error(ErrorType errorType, T data) { | ||
return new ApiResponse<>(errorType.getCode(), errorType.getMessage(), data); | ||
} | ||
|
||
public static ApiResponse<?> error(ErrorType errorType, String message) { | ||
return new ApiResponse<>(errorType.getCode(), message); | ||
} | ||
|
||
public static <T> ApiResponse<T> error(ErrorType errorType, String message, T data) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ฌ๊ธฐ๋์! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return new ApiResponse<>(errorType.getCode(), message, data); | ||
} | ||
|
||
public static <T> ApiResponse<Exception> error(ErrorType errorType, Exception e) { | ||
return new ApiResponse<>(errorType.getCode(), errorType.getMessage(), e); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package slvtwn.khu.toyouserver.common; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public enum ErrorType { | ||
|
||
// 404 (Not Found) | ||
NOT_FOUND("TYU404", "Not Found"); | ||
|
||
private final String code; | ||
|
||
private final String message; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package slvtwn.khu.toyouserver.common; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class PageInfoResponse { | ||
private final int currentPage; | ||
|
||
private final int totalPage; | ||
|
||
private final int totalElements; | ||
|
||
public static PageInfoResponse of(int currentPage, int totalPage, int totalElements) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ชจ๋ ์์ฒญ์ด paging์ด ๋ค์ด๊ฐ์ง๋ ์์์, ์๋ต ํ์์ ์๋์ ๊ฐ์ด ์ ๋ฌ๋ฉ๋๋ค. ์ด๋ ์ค๊น์ ? ๐ค {
"timestamp": "2024.07.06 00:35:58",
"code": "TYU-200",
"message": "์๋ต ์ฑ๊ณต",
"data": {
"id": 1,
"name": "health",
"pageInfo": {
"currentPage": 1,
"totalPage": 1,
"totalElements": 1
}
}
} |
||
return new PageInfoResponse(currentPage, totalPage, totalElements); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package slvtwn.khu.toyouserver.common; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public enum SuccessType { | ||
|
||
// 200 (OK) | ||
OK("TYU200", "success"); | ||
|
||
private final String code; | ||
private final String message; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ฑ์ ์ฒด์ด๋์ผ๋ก ๋๋ฉด ๊น๋ํ ๊ฒ ๊ฐ์ต๋๋ค!