-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseApiResponse.java
60 lines (50 loc) · 2.05 KB
/
BaseApiResponse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package team05.integrated_feed_backend.common;
import org.springframework.http.HttpStatus;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import team05.integrated_feed_backend.common.code.StatusCode;
/**
* code, status, message 기본 응답 형식
**/
@Getter
@Schema(description = "기본 응답 형식")
public class BaseApiResponse<T> {
@Schema(description = "HTTP 상태 코드", example = "200")
private int code;
@Schema(description = "HTTP 상태", example = "OK")
private HttpStatus status;
@Schema(description = "응답 메세지", example = "요청이 성공했습니다.")
private String message;
@Schema(description = "응답 데이터")
private final T data;
public BaseApiResponse(HttpStatus status, String message, T data) {
this.code = status.value();
this.status = status;
this.message = message;
this.data = data;
}
// 상태, 메세지 정의해주는 경우
public static <T> BaseApiResponse<T> of(HttpStatus httpStatus, String message, T data) {
return new BaseApiResponse<>(httpStatus, message, data);
}
// 상태만 정의, 메세지는 디폴트 메세지 사용하는 경우
public static BaseApiResponse<Void> of(HttpStatus httpStatus) {
return of(httpStatus, HttpStatus.valueOf(httpStatus.value()).getReasonPhrase(), null);
}
// 상태, 메세지만 정의하는 경우
public static BaseApiResponse<Void> of(HttpStatus httpStatus, String message) {
return of(httpStatus, message, null);
}
// 상태 코드로 정의하는 경우
public static BaseApiResponse<Void> of(StatusCode statusCode) {
return of(statusCode.getHttpStatus(), statusCode.getMessage(), null);
}
// 상태, 데이터를 통해 정의하는 경우
public static <T> BaseApiResponse<T> of(HttpStatus httpStatus, T data) {
return of(httpStatus, HttpStatus.valueOf(httpStatus.value()).getReasonPhrase(), data);
}
// 상태 코드, 데이터를 통해 정의하는 경우
public static <T> BaseApiResponse<T> of(StatusCode statusCode, T data) {
return of(statusCode.getHttpStatus(), statusCode.getMessage(), data);
}
}