Skip to content

Commit dfeb99c

Browse files
committed
@ControllerAdvice
1 parent ac4c019 commit dfeb99c

File tree

7 files changed

+98
-24
lines changed

7 files changed

+98
-24
lines changed

exception/src/main/java/hello/exception/api/ApiExceptionV2Controller.java

+21-20
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@
1313
@Slf4j
1414
public class ApiExceptionV2Controller {
1515

16-
@ResponseStatus(HttpStatus.BAD_REQUEST)
17-
@ExceptionHandler(IllegalArgumentException.class) //생략가능 - 매개변수로 지정가능
18-
public ErrorResult illegalExHandler(IllegalArgumentException e) {
19-
log.error("[exceptionHandler] ex", e);
20-
return new ErrorResult("BAD", e.getMessage());
21-
}
22-
23-
@ExceptionHandler
24-
public ResponseEntity<ErrorResult> userHandler(UserException e) {
25-
log.error("[exceptionHandler] ex", e);
26-
ErrorResult errorResult = new ErrorResult("USER-EX", e.getMessage());
27-
return new ResponseEntity(errorResult, HttpStatus.BAD_REQUEST);
28-
}
29-
30-
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //IllegalArgumentException, UserException을 제외한 예외는 여기서!
31-
@ExceptionHandler
32-
public ErrorResult exHandler(Exception e) {
33-
log.error("[exception] ex", e);
34-
return new ErrorResult("EX", e.getMessage());
35-
}
16+
// @ResponseStatus(HttpStatus.BAD_REQUEST)
17+
// @ExceptionHandler(IllegalArgumentException.class) //생략가능 - 매개변수로 지정가능
18+
// public ErrorResult illegalExHandler(IllegalArgumentException e) {
19+
// log.error("[exceptionHandler] ex", e);
20+
// return new ErrorResult("BAD", e.getMessage());
21+
// }
22+
//
23+
// @ExceptionHandler
24+
// public ResponseEntity<ErrorResult> userHandler(UserException e) {
25+
// log.error("[exceptionHandler] ex", e);
26+
// ErrorResult errorResult = new ErrorResult("USER-EX", e.getMessage());
27+
// return new ResponseEntity(errorResult, HttpStatus.BAD_REQUEST);
28+
// }
29+
//
30+
// @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //IllegalArgumentException, UserException을 제외한 예외는 여기서!
31+
// @ExceptionHandler
32+
// public ErrorResult exHandler(Exception e) {
33+
// log.error("[exception] ex", e);
34+
// return new ErrorResult("EX", e.getMessage());
35+
// }
3636

3737
@GetMapping("/api2/members/{id}")
3838
public MemberDto getMember(@PathVariable("id") String id) {
@@ -60,4 +60,5 @@ static class MemberDto {
6060
private String memberId;
6161
private String name;
6262
}
63+
6364
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package hello.exception.api;
2+
3+
import hello.exception.exception.UserException;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@Slf4j
12+
@RestController
13+
public class ApiExceptionV3Controller {
14+
15+
@GetMapping("/api3/members/{id}")
16+
public MemberDto getMember(@PathVariable("id") String id) {
17+
if (id.equals("ex")) {
18+
throw new RuntimeException("잘못된 사용자");
19+
}
20+
if (id.equals("bad")) {
21+
throw new IllegalArgumentException("잘못된 입력값");
22+
}
23+
if (id.equals("user-ex")) {
24+
throw new UserException("사용자 오류");
25+
}
26+
27+
return new MemberDto(id, "hello " + id);
28+
}
29+
30+
@Data //requiredArgsConstructor, getter, setter, toString, toHashCode
31+
@AllArgsConstructor //모든 필드를 매개변수로 받는 생성자 생성
32+
static class MemberDto {
33+
private String memberId;
34+
private String name;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package hello.exception.exhandler.advice;
2+
3+
import hello.exception.exception.UserException;
4+
import hello.exception.exhandler.ErrorResult;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
import org.springframework.web.bind.annotation.ResponseStatus;
10+
import org.springframework.web.bind.annotation.RestController;
11+
import org.springframework.web.bind.annotation.RestControllerAdvice;
12+
13+
@Slf4j
14+
@RestControllerAdvice(basePackages = "hello.exception.api")
15+
public class ExControllerAdvice {
16+
17+
@ResponseStatus(HttpStatus.BAD_REQUEST)
18+
@ExceptionHandler(IllegalArgumentException.class) //생략가능 - 매개변수로 지정가능
19+
public ErrorResult illegalExHandler(IllegalArgumentException e) {
20+
log.error("[exceptionHandler] ex", e);
21+
return new ErrorResult("BAD", e.getMessage());
22+
}
23+
24+
@ExceptionHandler
25+
public ResponseEntity<ErrorResult> userHandler(UserException e) {
26+
log.error("[exceptionHandler] ex", e);
27+
ErrorResult errorResult = new ErrorResult("USER-EX", e.getMessage());
28+
return new ResponseEntity(errorResult, HttpStatus.BAD_REQUEST);
29+
}
30+
31+
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //IllegalArgumentException, UserException을 제외한 예외는 여기서!
32+
@ExceptionHandler
33+
public ErrorResult exHandler(Exception e) {
34+
log.error("[exception] ex", e);
35+
return new ErrorResult("EX", e.getMessage());
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
server.error.whitelabel.enabled=true
1+
server.error.whitelabel.enabled=false
22
spring.messages.basename=messages
33
server.error.include-message=always
44
server.error.include-exception=true

jpashop/src/main/java/jpabook/jpashop/domain/Address.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public class Address {
1414
private String zipcode;
1515

1616
protected Address(){ //protected 로 생성자 생성 => jpa 기본 규약(값 변경이 불가능하게 만들어야함)
17-
17+
// 기본 생성자가 무조건 있어야함
1818
}
1919

20-
public Address(String city,String street,String zipcode){
20+
public Address(String city,String street,String zipcode){ //값타입은 변경불가능하도록 => 초기 객체생성 때 값 지정
2121
this.city=city;
2222
this.street=street;
2323
this.zipcode=zipcode;

jpashop/src/main/java/jpabook/jpashop/domain/Category.java

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public class Category {
3737
public void addChildCategory(Category child){
3838
this.child.add(child);
3939
child.setParent(this);
40-
4140
}
4241

4342
}

jpashop/src/main/java/jpabook/jpashop/domain/Order.java

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class Order {
3232

3333
private LocalDateTime orderDate; //시간 정보
3434

35+
@Enumerated(EnumType.STRING)
3536
private OrderStatus status;
3637

3738
//연관관계 메소드(양방향 매핑 시, 값을 한번에 넣어주기 위한 메소드) ----------------------------------------------

0 commit comments

Comments
 (0)