package com.gk.hotwork.Domain.Exception.Handler;
|
|
import com.gk.hotwork.Domain.Exception.BusinessException;
|
import com.gk.hotwork.Domain.Exception.E;
|
import com.gk.hotwork.Domain.Utils.Msg;
|
import org.apache.log4j.LogManager;
|
import org.apache.log4j.Logger;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
@ControllerAdvice
|
public class CustomExceptionHandler {
|
|
private Logger logger = LogManager.getLogger(CustomExceptionHandler.class);
|
@ResponseBody
|
@ExceptionHandler(value = BusinessException.class)
|
public Msg errorHandler(BusinessException ex){
|
logger.warn(ex);
|
Msg msg = new Msg();
|
msg.setCode("400");
|
msg.setMessage(ex.getMessage());
|
return msg;
|
}
|
/**
|
* 处理入参异常
|
* @param e
|
* @return
|
*/
|
@ResponseBody
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
public Msg handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
|
logger.warn(e.getBindingResult().getFieldError().getDefaultMessage());
|
return new Msg(E.DATA_PARAM_NULL.getCode(),e.getBindingResult().getFieldError().getDefaultMessage());
|
}
|
}
|