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
8 changes: 8 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,16 @@
package com.example.week2.builder;

import com.example.week2.exception.CustomException;
import com.example.week2.exception.ErrorCode;
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("error");

throw new CustomException(ErrorCode.INVALID_REQUEST);
}
}
14 changes: 13 additions & 1 deletion src/main/java/com/example/week2/builder/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@
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;
private String school;
}

public static void main(String[] args) {
Student student = Student.builder()
.name("홍길동")
.age(20)
.school("세종대학교")
.build();

System.out.println(student);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public CustomException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.errorCode = errorCode;
}
}
}
4 changes: 2 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,8 @@
@AllArgsConstructor
public enum ErrorCode {

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

INVALID_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청이 들어왔습니다"),
SEJONG_UNI(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 throwCustomException() {
throw new CustomException(ErrorCode.INVALID_REQUEST);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
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;

//body 반환
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

//RestControllerAdvice 선언하고
//ExceptionHandler 해당 에러 클랫스로 선언하고
//로드 에러 찍고
//메세지 반환

@ExceptionHandler(NullPointerException.class)
public String handleNullPointerException() {
log.error("NullPointer Exception 처리 시작");
Expand All @@ -19,4 +26,24 @@ public String handleInternalError() {
log.error("InternalError 처리 시작");
return "InternalError 핸들링";
}

@ExceptionHandler(CustomException.class)
public String handleCustomException() {
log.error("CustomError 처리 시작");
return "CustomException 핸들링";
}

//과제
@ExceptionHandler(CustomException.class)
public ResponseEntity<ErrorResponse> handleCustomException(CustomException e) {
log.error("CustomeException 발생 : {}", 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 @@ -4,10 +4,13 @@
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

//View를 반환
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler2 {

//handler가 ILLegalAru~~ 에로를 자바준다. 잡고 싶은 에러.class로 선언
//@controller여서 view에서 찾고
@ExceptionHandler(IllegalArgumentException.class)
public String handleIllegalArgumentException() {
log.error("IllegalArgumentException 발생");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/example/week2/logger/LoggerExample1.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ public static void main(String[] args) {
LOGGER.info("Info입니다");
LOGGER.warn("Warn입니다");
LOGGER.error("Error입니다");

//trace : 매우 상세한 디버깅 정보
//debug : 디버깅에 유용한 정보
//............................
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/example/week2/logger/LoggerExample2.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@Slf4j
public class LoggerExample2 {

//어노테이션 사용해서 자동으로 log 생성
public static void main(String[] args) {
log.trace("Trace입니다");
log.debug("Debug입니다");
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/example/week2/logger/LoggerExample3.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ public static void main(String[] args) {
.school("세종대학교")
.build();

log.info("학생 이름은" + student.getName() + "입니다");
log.info("학생 이름은" + student.getName() + "입니다"); //로그 레벨에 상관없이 항상 출력됨 + 계산이 처리된다.

log.info("학생 이름은 {} 입니다.", student.getName());
//SLF4J 포매팅 방식
//INFO 레벨로 출력될 때만 호출되어 성능 최적화
}
}