-
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 pull request #10 from Lunawood/master
fix(controller, response): Success 성공시 객체로 반환
- Loading branch information
Showing
14 changed files
with
175 additions
and
179 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
docker-test-server/src/main/java/com/example/server/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,33 @@ | ||
package com.example.server.Response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class BaseResponse<T> { | ||
private final int status; | ||
private final String message; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
private final T data; | ||
|
||
|
||
public static <T> BaseResponse<T> success(SuccessBase success, T data) { | ||
return new BaseResponse<>(success.getStatus(), success.getMessage(), data); | ||
} | ||
|
||
public static <T> BaseResponse<T> error(ErrorBase error) { | ||
return new BaseResponse<>(error.getStatus(), error.getMessage(), null); | ||
} | ||
|
||
public static <T> BaseResponse<T> error(ErrorBase error, T data) { | ||
return new BaseResponse<>(error.getStatus(), error.getMessage(), data); | ||
} | ||
|
||
public static BaseResponse<Exception> error(ErrorBase error, Exception exception) { | ||
return new BaseResponse<>(error.getStatus(), error.getMessage(), exception); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
docker-test-server/src/main/java/com/example/server/Response/CException.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,13 @@ | ||
package com.example.server.Response; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CException extends RuntimeException { | ||
private final ErrorBase errorBase; | ||
|
||
public CException(ErrorBase errorBase) { | ||
super(errorBase.getMessage()); | ||
this.errorBase = errorBase; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
docker-test-server/src/main/java/com/example/server/Response/CExceptionHandler.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,37 @@ | ||
package com.example.server.Response; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class CExceptionHandler { | ||
@ExceptionHandler(CException.class) | ||
protected ResponseEntity<ErrorBase> handleCustomException(CException error) { | ||
return ResponseEntity | ||
.status(error.getErrorBase().getStatus()) | ||
.body(error.getErrorBase()); | ||
} | ||
|
||
// Null 값 처리 | ||
@ExceptionHandler(NullPointerException.class) | ||
public ResponseEntity<BaseResponse<ErrorBase>> handleNullPointerException() { | ||
BaseResponse<ErrorBase> response = BaseResponse.error(ErrorBase.NULL_VALUE); | ||
|
||
return ResponseEntity | ||
.status(ErrorBase.NULL_VALUE.getStatus()) | ||
.body(response); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
protected ResponseEntity<BaseResponse<Exception>> handleException(Exception exception) { | ||
BaseResponse<Exception> response = BaseResponse.error(ErrorBase.INTERNAL_SERVER_ERROR, exception); | ||
|
||
return ResponseEntity | ||
.status(ErrorBase.INTERNAL_SERVER_ERROR.getStatus()) | ||
.body(response); | ||
} | ||
} |
7 changes: 2 additions & 5 deletions
7
...a/com/example/server/error/ErrorCode.java → ...om/example/server/Response/ErrorBase.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
13 changes: 13 additions & 0 deletions
13
docker-test-server/src/main/java/com/example/server/Response/SuccessBase.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,13 @@ | ||
package com.example.server.Response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum SuccessBase { | ||
SUCCESS(200, "."); | ||
|
||
private final int status; | ||
private final String message; | ||
} |
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 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 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 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
Oops, something went wrong.