Skip to content

Commit

Permalink
Merge pull request #4 from PICK-PLE/setting/#3
Browse files Browse the repository at this point in the history
[setting] 에러 및 응답관련 초기셋팅
  • Loading branch information
bo-ram-bo-ram authored Jul 4, 2024
2 parents 553d444 + 0de9041 commit 9ab3383
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 5 deletions.
16 changes: 15 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = 'server'
rootProject.name = 'PickpleServer'
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Empty file.
57 changes: 57 additions & 0 deletions src/main/java/com/pickple/server/api/TestController.java
Original file line number Diff line number Diff line change
@@ -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<String> responseEntity() {
return ResponseEntity.ok("ok");
}

@GetMapping("/success1")
public ApiResponse<String> successWithData() {
return ApiResponse.success(SuccessCode.TEST_OK_SUCCESS, "data");
}

@GetMapping("/success2")
public ApiResponse<Void> successWithoutData() {
return ApiResponse.success(SuccessCode.TEST_CREATE_SUCCESS);
}


@GetMapping("/exception1")
public ApiResponse<Void> testCustomException() {
return ApiResponse.fail(ErrorCode.INTERNAL_SERVER_ERROR);
}

@GetMapping("/exception2")
public ApiResponse<Void> testException() {
throw new RuntimeException("This is a test exception");
}

@ExceptionHandler(CustomException.class)
public ResponseEntity<ApiResponse<Void>> handleCustomException(CustomException ex) {
ApiResponse<Void> response = ApiResponse.fail(ex.getErrorCode());
return new ResponseEntity<>(response, ex.getErrorCode().getHttpStatus());
}

@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ApiResponse<Void>> handleRuntimeException(RuntimeException ex) {
ApiResponse<Void> response = ApiResponse.fail(ErrorCode.TEST_ERROR);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<ErrorCode> handleBusinessException(CustomException e) {
// log.error("GlobalExceptionHandler catch CustomException : {}", e.getErrorCode().getMessage());
// return ResponseEntity
// .status(e.getErrorCode().getHttpStatus())
// .body(e.getErrorCode());
// }
//}
Original file line number Diff line number Diff line change
@@ -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<ApiResponse<?>> {

@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;
}
}
Empty file.
45 changes: 45 additions & 0 deletions src/main/java/com/pickple/server/global/response/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -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<T> (
@JsonIgnore HttpStatus httpStatus,
int status ,
@NonNull String message,
@JsonInclude(value = NON_NULL) T data
){
public static<T> ApiResponse<T> success(final SuccessCode successCode, @Nullable final T data) {
return ApiResponse.<T>builder()
.httpStatus(successCode.getHttpStatus())
.status(successCode.getCode())
.message(successCode.getMessage())
.data(data)
.build();
}
public static <T> ApiResponse<T> success(final SuccessCode successCode) {
return ApiResponse.<T>builder()
.httpStatus(successCode.getHttpStatus())
.status(successCode.getCode())
.message(successCode.getMessage())
.data(null)
.build();
}
public static <T> ApiResponse<T> fail(final ErrorCode errorCode) {
return ApiResponse.<T>builder()
.httpStatus(errorCode.getHttpStatus())
.status(errorCode.getCode())
.message(errorCode.getMessage())
.data(null)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 9ab3383

Please sign in to comment.