Repository navigation
feat: 공통 API 응답 형식 구현 #10
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
23 changes: 23 additions & 0 deletions
23
src/main/java/ject/official_qr_checkin_server/common/exception/BusinessException.java
This file contains hidden or 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 ject.official_qr_checkin_server.common.exception; | ||
|
|
||
| import java.io.Serial; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class BusinessException extends RuntimeException { | ||
|
|
||
| @Serial | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final ErrorCode errorCode; | ||
|
|
||
| public BusinessException(ErrorCode errorCode) { | ||
| this(errorCode, errorCode.getMessage()); | ||
| } | ||
|
|
||
| public BusinessException(ErrorCode errorCode, String message) { | ||
| super(message); | ||
| this.errorCode = errorCode; | ||
| } | ||
|
|
||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/ject/official_qr_checkin_server/common/exception/ErrorCode.java
This file contains hidden or 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,13 @@ | ||
| package ject.official_qr_checkin_server.common.exception; | ||
|
|
||
| import java.io.Serializable; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public interface ErrorCode extends Serializable { | ||
|
|
||
| HttpStatus getHttpStatus(); | ||
|
|
||
| String getCode(); | ||
|
|
||
| String getMessage(); | ||
| } |
67 changes: 67 additions & 0 deletions
67
src/main/java/ject/official_qr_checkin_server/common/exception/GlobalErrorCode.java
This file contains hidden or 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,67 @@ | ||
| package ject.official_qr_checkin_server.common.exception; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public enum GlobalErrorCode implements ErrorCode { | ||
|
|
||
| UNSUPPORTED_PARAMETER_TYPE( | ||
| HttpStatus.BAD_REQUEST, | ||
| "GLOBAL-001", | ||
| "요청 파라미터의 형식이 올바르지 않습니다." | ||
| ), | ||
| MISSING_REQUEST_PARAMETER( | ||
| HttpStatus.BAD_REQUEST, | ||
| "GLOBAL-002", | ||
| "필수 요청 파라미터가 누락되었습니다." | ||
| ), | ||
| MISSING_REQUEST_BODY( | ||
| HttpStatus.BAD_REQUEST, | ||
| "GLOBAL-003", | ||
| "요청 본문이 누락되었거나 형식이 올바르지 않습니다." | ||
| ), | ||
| VALIDATION_FAILED( | ||
| HttpStatus.BAD_REQUEST, | ||
| "GLOBAL-004", | ||
| "요청 값 검증에 실패했습니다." | ||
| ), | ||
| METHOD_NOT_ALLOWED( | ||
| HttpStatus.METHOD_NOT_ALLOWED, | ||
| "GLOBAL-005", | ||
| "지원하지 않는 HTTP 메서드입니다." | ||
| ), | ||
| RESOURCE_NOT_FOUND( | ||
| HttpStatus.NOT_FOUND, | ||
| "GLOBAL-006", | ||
| "요청한 리소스를 찾을 수 없습니다." | ||
| ), | ||
| INTERNAL_SERVER_ERROR( | ||
| HttpStatus.INTERNAL_SERVER_ERROR, | ||
| "GLOBAL-999", | ||
| "서버 내부 오류가 발생했습니다." | ||
| ); | ||
|
|
||
| private final HttpStatus httpStatus; | ||
| private final String code; | ||
| private final String message; | ||
|
|
||
| GlobalErrorCode(HttpStatus httpStatus, String code, String message) { | ||
| this.httpStatus = httpStatus; | ||
| this.code = code; | ||
| this.message = message; | ||
| } | ||
|
|
||
| @Override | ||
| public HttpStatus getHttpStatus() { | ||
| return httpStatus; | ||
| } | ||
|
|
||
| @Override | ||
| public String getCode() { | ||
| return code; | ||
| } | ||
|
|
||
| @Override | ||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } |
126 changes: 126 additions & 0 deletions
126
src/main/java/ject/official_qr_checkin_server/common/exception/GlobalExceptionHandler.java
This file contains hidden or 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,126 @@ | ||
| package ject.official_qr_checkin_server.common.exception; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import ject.official_qr_checkin_server.common.response.ApiResponse; | ||
| import ject.official_qr_checkin_server.common.response.ErrorResponse; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.context.MessageSourceResolvable; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.http.converter.HttpMessageNotReadableException; | ||
| import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
| import org.springframework.web.bind.MethodArgumentNotValidException; | ||
| import org.springframework.web.bind.MissingServletRequestParameterException; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
| import org.springframework.web.method.annotation.HandlerMethodValidationException; | ||
| import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; | ||
| import org.springframework.web.servlet.resource.NoResourceFoundException; | ||
|
|
||
| @RestControllerAdvice | ||
| public class GlobalExceptionHandler { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); | ||
|
|
||
| @ExceptionHandler(BusinessException.class) | ||
| ResponseEntity<ApiResponse<List<String>>> handleBusinessException(BusinessException exception) { | ||
| ErrorCode errorCode = exception.getErrorCode(); | ||
| logKnownException(exception, errorCode); | ||
| return createResponse(ErrorResponse.of(errorCode, exception.getMessage())); | ||
| } | ||
|
|
||
| @ExceptionHandler(MethodArgumentTypeMismatchException.class) | ||
| ResponseEntity<ApiResponse<List<String>>> handleMethodArgumentTypeMismatch( | ||
| MethodArgumentTypeMismatchException exception | ||
| ) { | ||
| return handleKnownException(exception, GlobalErrorCode.UNSUPPORTED_PARAMETER_TYPE); | ||
| } | ||
|
|
||
| @ExceptionHandler(MissingServletRequestParameterException.class) | ||
| ResponseEntity<ApiResponse<List<String>>> handleMissingRequestParameter( | ||
| MissingServletRequestParameterException exception | ||
| ) { | ||
| return handleKnownException(exception, GlobalErrorCode.MISSING_REQUEST_PARAMETER); | ||
| } | ||
|
|
||
| @ExceptionHandler(HttpMessageNotReadableException.class) | ||
| ResponseEntity<ApiResponse<List<String>>> handleMessageNotReadable( | ||
| HttpMessageNotReadableException exception | ||
| ) { | ||
| return handleKnownException(exception, GlobalErrorCode.MISSING_REQUEST_BODY); | ||
| } | ||
|
|
||
| @ExceptionHandler({ | ||
| MethodArgumentNotValidException.class, | ||
| HandlerMethodValidationException.class | ||
| }) | ||
| ResponseEntity<ApiResponse<List<String>>> handleValidationException(Exception exception) { | ||
| GlobalErrorCode errorCode = GlobalErrorCode.VALIDATION_FAILED; | ||
| logKnownException(exception, errorCode); | ||
|
|
||
| List<String> messages = switch (exception) { | ||
| case MethodArgumentNotValidException methodArgumentNotValidException -> | ||
| extractMessages(methodArgumentNotValidException.getAllErrors(), errorCode); | ||
| case HandlerMethodValidationException handlerMethodValidationException -> | ||
| extractMessages(handlerMethodValidationException.getAllErrors(), errorCode); | ||
| default -> List.of(errorCode.getMessage()); | ||
| }; | ||
|
|
||
| return createResponse(ErrorResponse.of(errorCode, messages)); | ||
| } | ||
|
|
||
| @ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||
| ResponseEntity<ApiResponse<List<String>>> handleMethodNotSupported( | ||
| HttpRequestMethodNotSupportedException exception | ||
| ) { | ||
| return handleKnownException(exception, GlobalErrorCode.METHOD_NOT_ALLOWED); | ||
| } | ||
|
|
||
| @ExceptionHandler(NoResourceFoundException.class) | ||
| ResponseEntity<ApiResponse<List<String>>> handleResourceNotFound(NoResourceFoundException exception) { | ||
| return handleKnownException(exception, GlobalErrorCode.RESOURCE_NOT_FOUND); | ||
| } | ||
|
|
||
| @ExceptionHandler(Exception.class) | ||
| ResponseEntity<ApiResponse<List<String>>> handleUnexpectedException(Exception exception) { | ||
| GlobalErrorCode errorCode = GlobalErrorCode.INTERNAL_SERVER_ERROR; | ||
| log.error("Unhandled exception. responseCode={}", errorCode.getCode(), exception); | ||
| return createResponse(ErrorResponse.of(errorCode)); | ||
| } | ||
|
|
||
| private ResponseEntity<ApiResponse<List<String>>> handleKnownException( | ||
| Exception exception, | ||
| ErrorCode errorCode | ||
| ) { | ||
| logKnownException(exception, errorCode); | ||
| return createResponse(ErrorResponse.of(errorCode)); | ||
| } | ||
|
|
||
| private List<String> extractMessages( | ||
| List<? extends MessageSourceResolvable> errors, | ||
| ErrorCode fallbackErrorCode | ||
| ) { | ||
| List<String> messages = errors.stream() | ||
| .map(MessageSourceResolvable::getDefaultMessage) | ||
| .filter(Objects::nonNull) | ||
| .distinct() | ||
| .toList(); | ||
|
|
||
| return messages.isEmpty() ? List.of(fallbackErrorCode.getMessage()) : messages; | ||
| } | ||
|
|
||
| private ResponseEntity<ApiResponse<List<String>>> createResponse(ErrorResponse errorResponse) { | ||
| return ResponseEntity | ||
| .status(errorResponse.httpStatus()) | ||
| .body(errorResponse.toApiResponse()); | ||
| } | ||
|
|
||
| private void logKnownException(Exception exception, ErrorCode errorCode) { | ||
| log.warn( | ||
| "Handled exception. type={}, responseCode={}", | ||
| exception.getClass().getSimpleName(), | ||
| errorCode.getCode() | ||
| ); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/ject/official_qr_checkin_server/common/response/ApiResponse.java
This file contains hidden or 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,20 @@ | ||
| package ject.official_qr_checkin_server.common.response; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| public record ApiResponse<T>( | ||
| String status, | ||
| T data, | ||
| Instant timestamp | ||
| ) { | ||
|
|
||
| private static final String SUCCESS_STATUS = "SUCCESS"; | ||
|
|
||
| public static <T> ApiResponse<T> success(T data) { | ||
| return new ApiResponse<>(SUCCESS_STATUS, data, Instant.now()); | ||
| } | ||
|
|
||
| public static <T> ApiResponse<T> error(String status, T data) { | ||
| return new ApiResponse<>(status, data, Instant.now()); | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
src/main/java/ject/official_qr_checkin_server/common/response/ApiResponseBodyAdvice.java
This file contains hidden or 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,71 @@ | ||
| package ject.official_qr_checkin_server.common.response; | ||
|
|
||
| import java.util.List; | ||
| import org.springframework.core.MethodParameter; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.ProblemDetail; | ||
| import org.springframework.http.converter.AbstractJacksonHttpMessageConverter; | ||
| import org.springframework.http.converter.HttpMessageConverter; | ||
| import org.springframework.http.server.ServerHttpRequest; | ||
| import org.springframework.http.server.ServerHttpResponse; | ||
| import org.springframework.http.server.ServletServerHttpResponse; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
| import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; | ||
|
|
||
| @RestControllerAdvice | ||
| public class ApiResponseBodyAdvice implements ResponseBodyAdvice<Object> { | ||
|
|
||
| private static final List<String> EXCLUDED_PATH_PREFIXES = List.of( | ||
| "/actuator", | ||
| "/v3/api-docs", | ||
| "/swagger-ui" | ||
| ); | ||
|
|
||
| @Override | ||
| public boolean supports( | ||
| MethodParameter returnType, | ||
| Class<? extends HttpMessageConverter<?>> converterType | ||
| ) { | ||
| return AbstractJacksonHttpMessageConverter.class.isAssignableFrom(converterType); | ||
| } | ||
|
|
||
| @Override | ||
| public Object beforeBodyWrite( | ||
| Object body, | ||
| MethodParameter returnType, | ||
| MediaType selectedContentType, | ||
| Class<? extends HttpMessageConverter<?>> selectedConverterType, | ||
| ServerHttpRequest request, | ||
| ServerHttpResponse response | ||
| ) { | ||
| if (shouldSkip(body, request, response)) { | ||
| return body; | ||
| } | ||
|
|
||
| return ApiResponse.success(body); | ||
| } | ||
|
|
||
| private boolean shouldSkip(Object body, ServerHttpRequest request, ServerHttpResponse response) { | ||
| return body instanceof ApiResponse<?> | ||
| || body instanceof ProblemDetail | ||
| || isExcludedPath(request.getURI().getPath()) | ||
| || isNonSuccessfulOrEmptyResponse(response); | ||
| } | ||
|
|
||
| private boolean isExcludedPath(String path) { | ||
| return EXCLUDED_PATH_PREFIXES.stream().anyMatch(path::startsWith); | ||
| } | ||
|
|
||
| private boolean isNonSuccessfulOrEmptyResponse(ServerHttpResponse response) { | ||
| if (!(response instanceof ServletServerHttpResponse servletResponse)) { | ||
| return false; | ||
| } | ||
|
|
||
| int status = servletResponse.getServletResponse().getStatus(); | ||
| return status < 200 | ||
| || status >= 300 | ||
| || status == HttpStatus.NO_CONTENT.value() | ||
| || status == HttpStatus.RESET_CONTENT.value(); | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
src/main/java/ject/official_qr_checkin_server/common/response/ErrorResponse.java
This file contains hidden or 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,32 @@ | ||
| package ject.official_qr_checkin_server.common.response; | ||
|
|
||
| import java.util.List; | ||
| import ject.official_qr_checkin_server.common.exception.ErrorCode; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public record ErrorResponse( | ||
| HttpStatus httpStatus, | ||
| String code, | ||
| List<String> messages | ||
| ) { | ||
|
|
||
| public ErrorResponse { | ||
| messages = List.copyOf(messages); | ||
| } | ||
|
|
||
| public static ErrorResponse of(ErrorCode errorCode) { | ||
| return of(errorCode, List.of(errorCode.getMessage())); | ||
| } | ||
|
|
||
| public static ErrorResponse of(ErrorCode errorCode, String message) { | ||
| return of(errorCode, List.of(message)); | ||
| } | ||
|
|
||
| public static ErrorResponse of(ErrorCode errorCode, List<String> messages) { | ||
| return new ErrorResponse(errorCode.getHttpStatus(), errorCode.getCode(), messages); | ||
| } | ||
|
|
||
| public ApiResponse<List<String>> toApiResponse() { | ||
| return ApiResponse.error(code, messages); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
제외 경로를 경계 단위로 판별하세요.
startsWith는/actuator-health나/v3/api-docs-preview도 제외합니다. 정확한 경로 또는prefix + "/"하위 경로만 제외해야 합니다.수정 예시
private boolean isExcludedPath(String path) { - return EXCLUDED_PATH_PREFIXES.stream().anyMatch(path::startsWith); + return EXCLUDED_PATH_PREFIXES.stream() + .anyMatch(prefix -> path.equals(prefix) || path.startsWith(prefix + "/")); }📝 Committable suggestion
🤖 Prompt for AI Agents