Web Application 의 입장에서 바라보았을 때 에러가 났을 때 내려주는 방법은 많지 않다.
User.java
package com.example.exception.dto;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class User {
@NotEmpty
@Size(min = 1, max = 10)
private String name;
@Min(1)
@NotNull
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
ApiController.java
package com.example.exception.controller;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.exception.dto.User;
@RestController
@RequestMapping("/api/user")
public class ApiController {
@GetMapping("")
// (required=false) 값이 들어오지 않아도 실행은 하되 null 값으로 들어옴 , true면 실행 불가
public User get(@RequestParam(required=false) String name, @RequestParam(required=false) Integer age){
User user = new User();
user.setName(name);
user.setAge(age);
int a = 10 + age;
return user;
}
@PostMapping("")
public User post(@Valid @RequestBody User user) {
System.out.println(user);
return user;
}
}
GlobalControllerAdvice.java
package com.example.exception.advice;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
// exception 처리
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice // Restapi 에 사용
// @RestControllerAdvice(basePackageClasses = ApiController.class) // 특정 패키지.class 안에서만 사용하는 예외처리 advice (global동작 X)
// @ControllerAdvice //view 에 사용하는 어노테이션
public class GlobalControllerAdvice {
@ExceptionHandler(value = Exception.class) // class에서 일어나는 모든 exception 을 모두 처리
public ResponseEntity exception(Exception e) { //(Exception e)를 설정하면 위에서 받은 exception을 모두 받을 수 있음!
System.out.println(e.getClass().getName());
System.out.println("=========================");
System.out.println(e.getLocalizedMessage());
System.out.println("=========================");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("");
}
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public ResponseEntity methodArgumentNotValidException(MethodArgumentNotValidException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}
@ExceptionHandler(value = Exception.class) 결과
@ExceptionHandler(value = MethodArgumentNotValidException.class) 결과
ApiController.java - 아래 함수 추가
즉 , GlobalController 를 지정하더라도 class 내의 exception 먼저 탄다!
//GlobalController 를 지정하더라도 class 내의 exception 먼저 탄다!
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public ResponseEntity methodArgumentNotValidException(MethodArgumentNotValidException e) {
System.out.println("api controller exception");
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}