From a3dbf78f41dd11a5ebcfdddfa797bbff66ce3b6c Mon Sep 17 00:00:00 2001 From: bo-ram-bo-ram Date: Thu, 4 Jul 2024 20:15:49 +0900 Subject: [PATCH 01/10] =?UTF-8?q?fix=20:=20cors=20config=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/pickple/server/global/config/WebConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/pickple/server/global/config/WebConfig.java b/src/main/java/com/pickple/server/global/config/WebConfig.java index 518bd93b..c5785da2 100644 --- a/src/main/java/com/pickple/server/global/config/WebConfig.java +++ b/src/main/java/com/pickple/server/global/config/WebConfig.java @@ -9,7 +9,7 @@ public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") - .allowedOrigins("*") + .allowedOrigins("http://localhost:8080", "http://localhost:8081", "http://localhost:5173") .allowedMethods("GET", "POST", "PUT", "DELETE") .allowCredentials(true) .maxAge(3000); From abd7fb4ea2e9461a3b29457009937b0b6b423bd6 Mon Sep 17 00:00:00 2001 From: bo-ram-bo-ram Date: Thu, 4 Jul 2024 20:16:19 +0900 Subject: [PATCH 02/10] =?UTF-8?q?chore=20:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=20=ED=8C=8C=EC=9D=BC=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/pickple/server/global/exception/enums/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/java/com/pickple/server/global/exception/enums/.gitkeep diff --git a/src/main/java/com/pickple/server/global/exception/enums/.gitkeep b/src/main/java/com/pickple/server/global/exception/enums/.gitkeep deleted file mode 100644 index e69de29b..00000000 From 0a3d71ce1a80d01f43b9101b559e720eb73ed2bc Mon Sep 17 00:00:00 2001 From: bo-ram-bo-ram Date: Thu, 4 Jul 2024 20:16:53 +0900 Subject: [PATCH 03/10] =?UTF-8?q?fix=20:=20dependencies=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 63b5d9b3..e20e7957 100644 --- a/build.gradle +++ b/build.gradle @@ -24,13 +24,27 @@ repositories { } dependencies { - implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + // Spring implementation 'org.springframework.boot:spring-boot-starter-web' + developmentOnly 'org.springframework.boot:spring-boot-devtools' implementation 'org.springframework.boot:spring-boot-starter-actuator' + + //db implementation group: 'org.postgresql', name: 'postgresql', version: '42.7.3' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + + + // Lombok compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' + + // Validation + implementation 'org.springframework.boot:spring-boot-starter-validation' + + // Test testImplementation 'org.springframework.boot:spring-boot-starter-test' + + //junit testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } From 109a61ac14607a48ff4562f1a9c6353a7f5cc744 Mon Sep 17 00:00:00 2001 From: bo-ram-bo-ram Date: Thu, 4 Jul 2024 20:17:27 +0900 Subject: [PATCH 04/10] =?UTF-8?q?feat=20:=20GlobalExceptionHandler=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/GlobalExceptionHandler.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/main/java/com/pickple/server/global/exception/GlobalExceptionHandler.java b/src/main/java/com/pickple/server/global/exception/GlobalExceptionHandler.java index 41fd6d19..95a4ce53 100644 --- a/src/main/java/com/pickple/server/global/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/pickple/server/global/exception/GlobalExceptionHandler.java @@ -1,4 +1,40 @@ package com.pickple.server.global.exception; +import com.pickple.server.global.response.enums.ErrorCode; +import com.pickple.server.global.response.ApiResponse; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.NoHandlerFoundException; + +@Slf4j +@RestControllerAdvice public class GlobalExceptionHandler { + // 존재하지 않는 요청에 대한 예외 + @ExceptionHandler(value = {NoHandlerFoundException.class, HttpRequestMethodNotSupportedException.class}) + public ApiResponse handleNoPageFoundException(Exception e) { + log.error("GlobalExceptionHandler catch NoHandlerFoundException : {}", e.getMessage()); + return ApiResponse.fail(ErrorCode.NOT_FOUND_END_POINT); + } + + // 기본 예외 + @ExceptionHandler(value = {Exception.class}) + public ApiResponse handleException(Exception e) { + log.error("handleException() in GlobalExceptionHandler throw Exception : {}", e.getMessage()); + e.printStackTrace(); + return ApiResponse.fail(ErrorCode.INTERNAL_SERVER_ERROR); + } } +//@Slf4j +//@RestControllerAdvice +//public class GlobalExceptionHandler { +// // 비즈니스 로직에서 발생한 예외 +// @ExceptionHandler(CustomException.class) +// public ResponseEntity handleBusinessException(CustomException e) { +// log.error("GlobalExceptionHandler catch CustomException : {}", e.getErrorCode().getMessage()); +// return ResponseEntity +// .status(e.getErrorCode().getHttpStatus()) +// .body(e.getErrorCode()); +// } +//} From 60aaf2cc2272d5023a7fbabbc350ba541475b652 Mon Sep 17 00:00:00 2001 From: bo-ram-bo-ram Date: Thu, 4 Jul 2024 20:24:57 +0900 Subject: [PATCH 05/10] =?UTF-8?q?feat=20:=20ResponseHeader=EC=9D=98=20http?= =?UTF-8?q?=20status=20code=EB=A5=BC=20=EC=97=90=EB=9F=AC=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=EC=99=80=20=EC=9D=BC=EC=B9=98=EB=90=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/ResponseStatusSetterAdvice.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/main/java/com/pickple/server/global/exception/ResponseStatusSetterAdvice.java diff --git a/src/main/java/com/pickple/server/global/exception/ResponseStatusSetterAdvice.java b/src/main/java/com/pickple/server/global/exception/ResponseStatusSetterAdvice.java new file mode 100644 index 00000000..18d9c60b --- /dev/null +++ b/src/main/java/com/pickple/server/global/exception/ResponseStatusSetterAdvice.java @@ -0,0 +1,35 @@ +package com.pickple.server.global.exception; + +import com.pickple.server.global.response.ApiResponse; +import org.springframework.core.MethodParameter; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; + + +@RestControllerAdvice +public class ResponseStatusSetterAdvice implements ResponseBodyAdvice> { + + @Override + public boolean supports(MethodParameter returnType, Class converterType) { + return returnType.getParameterType() == ApiResponse.class; + } + + @Override + public ApiResponse beforeBodyWrite( + ApiResponse body, + MethodParameter returnType, + MediaType selectedContentType, + Class selectedConverterType, + ServerHttpRequest request, + ServerHttpResponse response + ) { + HttpStatus status = body.httpStatus(); + response.setStatusCode(status); + + return body; + } +} \ No newline at end of file From 721ec0507f597a04bd4cefc4cf875a9679b70851 Mon Sep 17 00:00:00 2001 From: bo-ram-bo-ram Date: Thu, 4 Jul 2024 20:25:20 +0900 Subject: [PATCH 06/10] =?UTF-8?q?feat=20:=20=EA=B3=B5=ED=86=B5=EC=9D=91?= =?UTF-8?q?=EB=8B=B5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/global/response/ApiResponse.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/com/pickple/server/global/response/ApiResponse.java diff --git a/src/main/java/com/pickple/server/global/response/ApiResponse.java b/src/main/java/com/pickple/server/global/response/ApiResponse.java new file mode 100644 index 00000000..a15e685d --- /dev/null +++ b/src/main/java/com/pickple/server/global/response/ApiResponse.java @@ -0,0 +1,45 @@ +package com.pickple.server.global.response; + +import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.pickple.server.global.response.enums.ErrorCode; +import com.pickple.server.global.response.enums.SuccessCode; +import jakarta.annotation.Nullable; +import lombok.Builder; +import lombok.NonNull; +import org.springframework.http.HttpStatus; + +@Builder +public record ApiResponse ( + @JsonIgnore HttpStatus httpStatus, + int status , + @NonNull String message, + @JsonInclude(value = NON_NULL) T data +){ + public static ApiResponse success(final SuccessCode successCode, @Nullable final T data) { + return ApiResponse.builder() + .httpStatus(successCode.getHttpStatus()) + .status(successCode.getCode()) + .message(successCode.getMessage()) + .data(data) + .build(); + } + public static ApiResponse success(final SuccessCode successCode) { + return ApiResponse.builder() + .httpStatus(successCode.getHttpStatus()) + .status(successCode.getCode()) + .message(successCode.getMessage()) + .data(null) + .build(); + } + public static ApiResponse fail(final ErrorCode errorCode) { + return ApiResponse.builder() + .httpStatus(errorCode.getHttpStatus()) + .status(errorCode.getCode()) + .message(errorCode.getMessage()) + .data(null) + .build(); + } +} From 4a63b24349bc98adb7e301aecf8aa84ab1f1ba99 Mon Sep 17 00:00:00 2001 From: bo-ram-bo-ram Date: Thu, 4 Jul 2024 20:25:51 +0900 Subject: [PATCH 07/10] =?UTF-8?q?feat=20:=20=EC=9D=91=EB=8B=B5=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20enum=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/response/enums/ErrorCode.java | 20 +++++++++++++++++++ .../global/response/enums/SuccessCode.java | 16 +++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/main/java/com/pickple/server/global/response/enums/ErrorCode.java create mode 100644 src/main/java/com/pickple/server/global/response/enums/SuccessCode.java diff --git a/src/main/java/com/pickple/server/global/response/enums/ErrorCode.java b/src/main/java/com/pickple/server/global/response/enums/ErrorCode.java new file mode 100644 index 00000000..e1e44203 --- /dev/null +++ b/src/main/java/com/pickple/server/global/response/enums/ErrorCode.java @@ -0,0 +1,20 @@ +package com.pickple.server.global.response.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +@AllArgsConstructor +public enum ErrorCode { + // Test Error + TEST_ERROR(10000, HttpStatus.BAD_REQUEST, "테스트 에러입니다."), + // 404 Not Found + NOT_FOUND_END_POINT(40400, HttpStatus.NOT_FOUND, "존재하지 않는 API입니다."), + // 500 Internal Server Error + INTERNAL_SERVER_ERROR(50000, HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부 오류입니다."); + + private final int code; + private final HttpStatus httpStatus; + private final String message; +} diff --git a/src/main/java/com/pickple/server/global/response/enums/SuccessCode.java b/src/main/java/com/pickple/server/global/response/enums/SuccessCode.java new file mode 100644 index 00000000..8e636260 --- /dev/null +++ b/src/main/java/com/pickple/server/global/response/enums/SuccessCode.java @@ -0,0 +1,16 @@ +package com.pickple.server.global.response.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +@AllArgsConstructor +public enum SuccessCode { + TEST_OK_SUCCESS(20000, HttpStatus.OK, "test 조회 성공"), + TEST_CREATE_SUCCESS(20100, HttpStatus.CREATED, "test 생성 성공"); + + private final int code; + private final HttpStatus httpStatus; + private final String message; +} From 7d92df1637a065ef24a6323ee9548390ac8dcc35 Mon Sep 17 00:00:00 2001 From: bo-ram-bo-ram Date: Thu, 4 Jul 2024 20:26:13 +0900 Subject: [PATCH 08/10] =?UTF-8?q?feat=20:=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/exception/CustomException.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/java/com/pickple/server/global/exception/CustomException.java diff --git a/src/main/java/com/pickple/server/global/exception/CustomException.java b/src/main/java/com/pickple/server/global/exception/CustomException.java new file mode 100644 index 00000000..12598130 --- /dev/null +++ b/src/main/java/com/pickple/server/global/exception/CustomException.java @@ -0,0 +1,19 @@ +package com.pickple.server.global.exception; + +import com.pickple.server.global.response.enums.ErrorCode; +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +public class CustomException extends RuntimeException{ + private final ErrorCode errorCode; + + public CustomException(ErrorCode errorCode) { + super(errorCode.getMessage()); + this.errorCode = errorCode; + } + + public HttpStatus getHttpStatus() { + return errorCode.getHttpStatus(); + } +} From 593c774d7ca37b5b9033a4afe6bbd686ba741265 Mon Sep 17 00:00:00 2001 From: bo-ram-bo-ram Date: Thu, 4 Jul 2024 20:26:42 +0900 Subject: [PATCH 09/10] =?UTF-8?q?feat=20:=20testController=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pickple/server/api/TestController.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/main/java/com/pickple/server/api/TestController.java diff --git a/src/main/java/com/pickple/server/api/TestController.java b/src/main/java/com/pickple/server/api/TestController.java new file mode 100644 index 00000000..e48fee40 --- /dev/null +++ b/src/main/java/com/pickple/server/api/TestController.java @@ -0,0 +1,57 @@ +package com.pickple.server.api; + +import com.pickple.server.global.exception.CustomException; +import com.pickple.server.global.response.enums.ErrorCode; +import com.pickple.server.global.response.ApiResponse; +import com.pickple.server.global.response.enums.SuccessCode; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/test") +public class TestController { + + @GetMapping("/responseEntity") + public ResponseEntity responseEntity() { + return ResponseEntity.ok("ok"); + } + + @GetMapping("/success1") + public ApiResponse successWithData() { + return ApiResponse.success(SuccessCode.TEST_OK_SUCCESS, "data"); + } + + @GetMapping("/success2") + public ApiResponse successWithoutData() { + return ApiResponse.success(SuccessCode.TEST_CREATE_SUCCESS); + } + + + @GetMapping("/exception1") + public ApiResponse testCustomException() { + return ApiResponse.fail(ErrorCode.INTERNAL_SERVER_ERROR); + } + + @GetMapping("/exception2") + public ApiResponse testException() { + throw new RuntimeException("This is a test exception"); + } + + @ExceptionHandler(CustomException.class) + public ResponseEntity> handleCustomException(CustomException ex) { + ApiResponse response = ApiResponse.fail(ex.getErrorCode()); + return new ResponseEntity<>(response, ex.getErrorCode().getHttpStatus()); + } + + @ExceptionHandler(RuntimeException.class) + public ResponseEntity> handleRuntimeException(RuntimeException ex) { + ApiResponse response = ApiResponse.fail(ErrorCode.TEST_ERROR); + return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); + } +} + From 0de9041cddf399dc31d28b08056d774115012d75 Mon Sep 17 00:00:00 2001 From: bo-ram-bo-ram Date: Thu, 4 Jul 2024 23:51:38 +0900 Subject: [PATCH 10/10] =?UTF-8?q?chore=20:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=82=AD=EC=A0=9C=20,=20project=20?= =?UTF-8?q?=EB=B0=8F=20application=20=EC=9D=B4=EB=A6=84=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- settings.gradle | 2 +- .../{ServerApplication.java => PickpleServerApplication.java} | 4 ++-- src/main/java/com/pickple/server/api/.gitkeep | 0 3 files changed, 3 insertions(+), 3 deletions(-) rename src/main/java/com/pickple/server/{ServerApplication.java => PickpleServerApplication.java} (68%) delete mode 100644 src/main/java/com/pickple/server/api/.gitkeep diff --git a/settings.gradle b/settings.gradle index 096502d2..2db0fe2f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -rootProject.name = 'server' +rootProject.name = 'PickpleServer' diff --git a/src/main/java/com/pickple/server/ServerApplication.java b/src/main/java/com/pickple/server/PickpleServerApplication.java similarity index 68% rename from src/main/java/com/pickple/server/ServerApplication.java rename to src/main/java/com/pickple/server/PickpleServerApplication.java index 6699eeda..89ea1fee 100644 --- a/src/main/java/com/pickple/server/ServerApplication.java +++ b/src/main/java/com/pickple/server/PickpleServerApplication.java @@ -4,10 +4,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class ServerApplication { +public class PickpleServerApplication { public static void main(String[] args) { - SpringApplication.run(ServerApplication.class, args); + SpringApplication.run(PickpleServerApplication.class, args); } } diff --git a/src/main/java/com/pickple/server/api/.gitkeep b/src/main/java/com/pickple/server/api/.gitkeep deleted file mode 100644 index e69de29b..00000000