1. 程式人生 > >springMVC統一異常處理

springMVC統一異常處理

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.xwind.core.bean.message.Message;
import com.xwind.core.bean.message.Messageable;
import com.xwind.core.exception.CoreException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestControllerAdvice
public class CoreExceptionHandler
{
    @ExceptionHandler(CoreException.class)
    @ResponseStatus(HttpStatus.OK)
    public Message handlerCoreException(CoreException e)
    {
        log.debug(e.getMessage());
        
        return new Message(e);
    }

    @ExceptionHandler(Throwable.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Message handlerThrowable(Throwable t)
    {
        log.debug(t.getMessage());
        
        return new Message(1, Messageable.MESSAGE_ERROR);
    }
}