SpringMVCグローバル例外処理

1228 ワード

import com.mmall.common.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Luyue
 * @date 2018/8/17 14:53
 **/
@Slf4j
@Component
public class ExceptionResolvor implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        log.error("{} is error", httpServletRequest.getRequestURI(), e);
        //jackson  2.0   MappingJacksonJsonView,2.0   MappingJackson2JsonView
        ModelAndView modelAndView = new ModelAndView(new MappingJacksonJsonView());
        modelAndView.addObject("status", ResponseCode.ERROR.getCode());
        modelAndView.addObject("msg", "           ,       ");
        modelAndView.addObject("data", e.toString());
        return modelAndView;
    }
}