@PheonixHkbxoic
2018-03-08T08:41:41.000000Z
字数 3080
阅读 1050
SpringBoot
spring:
mvc:
throw-exception-if-no-handler-found: true
@SpringBootApplication
@MapperScan("cn.pheker.springbootdemo.mapper")
@ServletComponentScan
@EnableTransactionManagement
@EnableWebMvc
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
package cn.pheker.springbootdemo.config;
import cn.pheker.springbootdemo.utils.Result;
import cn.pheker.springbootdemo.utils.UtilException;
import cn.pheker.springbootdemo.utils.UtilResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
* <pre>
* author cn.pheker
* date 2018/3/7 14:25
* email 1176479642@qq.com
* desc 全局异常处理器
*
* </pre>
*/
@ControllerAdvice
public class GlobalExceptionHandler {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@ExceptionHandler(Exception.class)
@ResponseBody
public Result<?> jsonErrorHandler(HttpServletRequest request, Exception e) {
Result result;
//无此页面或地址
if(e instanceof NoHandlerFoundException) {//404
return UtilResult.fail(UtilResult.TYPE.NOT_FOUND, request.getRequestURI());
}else if(e instanceof HttpRequestMethodNotSupportedException){
return UtilResult.fail(UtilResult.TYPE.METHOD_NOT_SUPPORTED, request.getRequestURI());
//缺少参数
}else if(e instanceof MissingServletRequestParameterException) {
result = UtilResult.fail(UtilResult.TYPE.PARAMETER_MISS, e.getMessage());
//参数类型不对或无法转换
}else if(e instanceof MethodArgumentTypeMismatchException){
MethodArgumentTypeMismatchException matme = (MethodArgumentTypeMismatchException) e;
result = UtilResult.fail(UtilResult.TYPE.PARAMETER_NOT_MATCH,matme.getMessage());
}else if(e instanceof MethodArgumentNotValidException){//参数校验
List<Result<String>> errorResults = new ArrayList<Result<String>>();
for (FieldError fieldError : ((MethodArgumentNotValidException) e).getBindingResult().getFieldErrors()) {
errorResults.add(UtilResult.fail(Integer.parseInt(fieldError.getCode()),fieldError.getDefaultMessage(),
fieldError.getField()+fieldError.getRejectedValue()));
}
return UtilResult.fail(500,e.getMessage(),errorResults);
}else{
result = UtilException.getResult(request, e.getMessage());
logger.debug(result.toString());
}
e.printStackTrace();
logger.error(e.getMessage());
return result;
}
}
以上是采用注解的方法实现的,@ControllerAdvice表示处理Controller,
@ExceptionHandler指明要处理的异常和使用当前方法处理该异常
@ResponseBody表明返回json,不写表明反回页面或ModelAndView