Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ repositories {
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
// runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/example/week2/builder/App1.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package com.example.week2.builder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App1 {

private static final Logger log = LoggerFactory.getLogger(App1.class);

public static void main(String[] args) {

log.error("에러입니다.");
Student student = Student.builder()
.name("최수빈")
.age(25)
.school("세종대학교")
.build();

System.out.println(student);

}
}
2 changes: 2 additions & 0 deletions src/main/java/com/example/week2/builder/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
@Slf4j
public class Student {
private String name;
private int age;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/example/week2/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
@AllArgsConstructor
public enum ErrorCode {

INVALID_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청이 들어왔습니다"),;

INVALID_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청이 들어왔습니다"),
SEJONG_UNIT(HttpStatus.OK,"세종대 학생 등록이 되었습니다"),
CUSTOM_ERROR(HttpStatus.BAD_REQUEST, "커스텀 에러가 발생했습니다.");

private final HttpStatus status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public void throwIllegalArgumentException() {
throw new IllegalArgumentException();
}

@GetMapping("/custom")
public void method1(){
throw new CustomException(ErrorCode.CUSTOM_ERROR);
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.example.week2.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

@ExceptionHandler(NullPointerException.class)
@ExceptionHandler(NullPointerException.class) // null포인터익셉션을 잡아냄
public String handleNullPointerException() {
// 잡아낸 후 다음 코드 실행
log.error("NullPointer Exception 처리 시작");
return "NullPointer Exception 핸들링";
}
Expand All @@ -19,4 +21,18 @@ public String handleInternalError() {
log.error("InternalError 처리 시작");
return "InternalError 핸들링";
}

@ExceptionHandler(CustomException.class)
public ResponseEntity<ErrorResponse> handleCustomException(CustomException e){
log.error("CustomException 발생: {}", e.getMessage(), e);

ErrorCode errorCode = e.getErrorCode();

ErrorResponse response = ErrorResponse.builder()
.errorCode(errorCode)
.errorMessage(errorCode.getMessage())
.build();

return ResponseEntity.status(errorCode.getStatus()).body(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Configuration // 환경설정
public class SwaggerConfig {

@Bean
Expand Down