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
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
languageVersion = JavaLanguageVersion.of(23)
}
}

Expand All @@ -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
15 changes: 15 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,23 @@
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) {

Student.StudentBuilder school = Student.builder()
.name("heeun")
.age(28)
.school("sejong");

Student st = school.build();


}
}
12 changes: 11 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,25 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class Student {

private Long id;
private String name;
private int age;
private String school;

@Builder
public Student(String name, int age, String school) {
this.name = name;
this.age = age;
this.school = school;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.Getter;

@Getter
public class CustomException extends RuntimeException{
public class CustomException extends RuntimeException{ // 런타임 상속

private final ErrorCode errorCode;

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/example/week2/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

@Getter
@AllArgsConstructor
public enum ErrorCode {
public enum ErrorCode { // 직접 커스텀

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



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

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



}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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 {
Expand All @@ -19,4 +21,23 @@ public String handleInternalError() {
log.error("InternalError 처리 시작");
return "InternalError 핸들링";
}


@ExceptionHandler(CustomException.class)
public ResponseEntity<ErrorResponse> handleCustomeException(CustomException e) {
log.info("customError 처리 시작 {}", 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,14 +4,15 @@
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

//
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler2 {

@ExceptionHandler(IllegalArgumentException.class)
public String handleIllegalArgumentException() {
log.error("IllegalArgumentException 발생");
return "error";
return "error";//error 라는 뷰를 찾는다. @ControllerAdvice 이기대문에
}

}
1 change: 1 addition & 0 deletions src/main/java/com/example/week2/logger/LoggerExample3.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static void main(String[] args) {
.build();

log.info("학생 이름은" + student.getName() + "입니다");
// logging level 을 error 을 해도 계산을 진행한다.

log.info("학생 이름은 {} 입니다.", student.getName());
}
Expand Down