스프링부트 with JPA - Exception 처리하기

최준호·2024년 2월 13일

Exception 페이지 설정

package com.junho.blog.handler;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

@ControllerAdvice
@RestController
public class GlobalExceptionHandler {
	
	@ExceptionHandler(value=Exception.class)
	public String handleArgumentException(Exception e) {
		return "<h1>"+e.getMessage()+"</h1>";
	}
}
  • 새로운 패키지를 만들고 Exception 처리를 하는 클래스를 생성해줍니다.

  • @ControllerAdvice : 는 모든 컨트롤러의 정보를 받아올 수 있는 어노테이션입니다.

  • @ExceptionHandler : value에 해당하는 에러에 대한 값을 처리를 할 수 있게 만들어주는 어노테이션입니다.

  • 결과값입니다
profile
변화를 두려워하는 사람이 가장 불행한 사람이다.

0개의 댓글