๋จผ์ response์ ๋ค์ด๊ฐ ๊ฐ๋ค์ ์ค์ ํด๋ณผ๊ฒ์!
1.๊ธฐ๋ณธ์ ์ธ httpStatus
2.์ฑ๊ณต์ธ์ง ์คํจ์ธ์ง ๋ช ํํ ํ๊ธฐ
3.์ฌ์ฉ์ ์ค์ code (ex. "MEM001")
4.์ธ๋ถ ๋ด์ฉ
@Getter
@Builder
public class ErrorReasonDTO {
private HttpStatus httpStatus;
private final boolean isSuccess;
private final String code;
private final String message;
public boolean getIsSuccess(){return isSuccess;}
}
@Getter
@Builder
public class ReasonDTO {
private HttpStatus httpStatus;
private final boolean isSuccess;
private final String code;
private final String message;
public boolean getIsSuccess(){return isSuccess;}
}
public interface BaseCode {
public ReasonDTO getReason();
public ReasonDTO getReasonHttpStatus();
}
public interface BaseErrorCode {
public ErrorReasonDTO getReason();
public ErrorReasonDTO getReasonHttpStatus();
}
@Getter
@AllArgsConstructor
public enum ErrorStatus implements BaseErrorCode {
_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON500", "์๋ฒ ๋ด๋ถ ์๋ฌ"),
_Bad_Request(HttpStatus.BAD_REQUEST, "COMMON400", "์๋ชป๋ ์์ฒญ์
๋๋ค"),
_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "COMMON401", "์ธ์ฆ์ด ํ์ํฉ๋๋ค"),
_FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "๊ธ์ง๋ ์์ฒญ์
๋๋ค"),
//๊ฐ์ฅ ์ผ๋ฐ์ ๊ธด ์๋ต
TEMP_EXCEPTION(HttpStatus.BAD_REQUEST, "TEMP40001", "์ด๊ฑฐ๋ ํ
์คํธ");
private final HttpStatus httpStatus;
private final String code;
private final String message;
@Override
public ErrorReasonDTO getReason() {
return ErrorReasonDTO.builder()
.message(message)
.code(code)
.isSuccess(false)
.build();
}
@Override
public ErrorReasonDTO getReasonHttpStatus() {
return ErrorReasonDTO.builder()
.message(message)
.code(code)
.httpStatus(httpStatus)
.isSuccess(false)
.build();
}
}
@Getter
@AllArgsConstructor
public enum SuccessStatus implements BaseCode{
_OK(HttpStatus.OK, "COMMON200", "์ฑ๊ณต์
๋๋ค");
private final HttpStatus httpStatus;
private final String code;
private final String message;
@Override
public ReasonDTO getReason() {
return ReasonDTO.builder()
.message(message)
.code(code)
.isSuccess(true)
.build();
}
@Override
public ReasonDTO getReasonHttpStatus() {
return ReasonDTO.builder()
.message(message)
.code(code)
.httpStatus(httpStatus)
.isSuccess(true)
.build();
}
}
@Getter
@AllArgsConstructor
@JsonPropertyOrder({"isSuccess", "code", "message", "result"})
public class ApiResponse<T> {
@JsonProperty("isSuccess")
private final Boolean isSuccess;
private final String code;
private final String message;
@JsonInclude(JsonInclude.Include.NON_NULL)
private T result;
public static <T> ApiResponse<T> onSuccess(T result) {
return new ApiResponse<>(true, SuccessStatus._OK.getCode(), SuccessStatus._OK.getMessage(), result);
}
public static <T> ApiResponse<T> onFailure(String code, String message, T data) {
return new ApiResponse<>(false, code, message, data);
}
}
@JsonProperty
์ด๋ ธํ ์ด์ ์ ๊ฐ์ฒด๋ฅผ JSON ํ์์ผ๋ก ๋ณํํ ๋ Key์ ์ด๋ฆ์ ์ค์ ํ ์ ์์ต๋๋ค.
ํด๋ผ์ด์ธํธ๋ ์ค๋ค์ดํฌ์ผ์ด์ค, ์๋ฒ๋ ์นด๋ฉ์ผ์ด์ค๋ก ์ํตํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๊ธฐ ๋๋ฌธ์ ๋ง์ถฐ์ฃผ๊ธฐ ์ํด ์ฌ์ฉํฉ๋๋ค!@JsonInclude(JsonInclude.Include.NON_NULL)
๋ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ์๋ง ๋ฐํ
test์์ ๋ฐํ๋ ๊ฐ์ฒด๋ฅผ ์ ์ธํด์ค๊ฒ์!
public class TempResponse {
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class TempResponseDTO {
String testString;
}
}
converter์์๋ dto๋ฅผ ์์ฑํด์ค๊ฒ์!
public class TempConverter {
public static TempResponse.TempResponseDTO toTempTestDTO() {
return TempResponse.TempResponseDTO.builder()
.testString("This is Test!")
.build();
}
}
controller ์์ฑํด ์ค๊ฒ์!
@RestController
@RequestMapping("/api/temp")
@RequiredArgsConstructor
public class TempController {
private final TempQueryService tempQueryService;
@GetMapping("/test")
public ApiResponse<TempResponse.TempResponseDTO> getTest() {
return ApiResponse.onSuccess(TempConverter.toTempTestDTO());
}
}