사용자 도구

사이트 도구


springframework:mvc:handlerexceptionresolver

HandlerExceptionResolver

기본 확장

@ControllerAdvice

package javabeat.net;
 
import java.io.IOException;
import java.sql.SQLException;
 
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 
@ControllerAdvice
public class ControllerAdviceTest {
    @ExceptionHandler(IOException.class)
    public ModelAndView handleIOException(IOException exception){
        ModelAndView andView = new ModelAndView();
        andView.setViewName("error");
        return andView;
    }
    @ExceptionHandler(SQLException.class)
    public ModelAndView handleSQLException(SQLException exception){
        ModelAndView andView = new ModelAndView();
        andView.setViewName("error");
        return andView;
    }
    // JSON 에러 내면서 Http Status Code 변경
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<SomeErrorObject> exceptionHandler(Exception e) {
        // org.springframework.http.HttpStatus 를 참조할 것.
        HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        SomeErrorObject errorObject = new SomeErrorObject();
 
        return new ResponseEntity<>(errorObject, httpStatus);
    }
}

주요 Spring MVC Exception에 대해 오버라이드 하지 말 것

참조

springframework/mvc/handlerexceptionresolver.txt · 마지막으로 수정됨: 2018/02/14 09:02 저자 kwon37xi