๋ฐ์ํ
GlobalExceptionHandler
package com.suri.farm.global.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;
import java.nio.file.AccessDeniedException;
import java.util.HashMap;
import java.util.Map;
@ControllerAdvice
public class GlobalExceptionHandler {
// 400 Bad Request
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, String>> handleBadRequest(MethodArgumentNotValidException e) {
e.printStackTrace();
Map<String, String> error = new HashMap<>();
error.put("message", "์๋ชป๋ ์์ฒญ์
๋๋ค. ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํ์ธ์");
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
}
// 500 Internal Server Error - ๊ทธ ์ธ ๋ชจ๋ ์์ธ
@ExceptionHandler(Exception.class)
public ResponseEntity<Map<String, String>> handleException(Exception e) {
e.printStackTrace();
Map<String, String> error = new HashMap<>();
error.put("message", "์๋ฒ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํ์ธ์.");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
}
}
์๋ ์๋ฌ์ฝ๋๋ ๋ฐ๋ก ๋ณ์๋ก ๋ฐ์ ์ ์๋๋ฐ ๊ท์ฐฎ์์...
e.printStackTrace();๊ฐ ์์ผ๋ฉด ์ฝ์์ ๋ญ๋๋ฉ ์๋ฌ๊ฐ ๋ํ๋ฌ๋์ง ์๋ํ๋๋ค.
๋ฐ์ํ