1 package cn.com.servyou.gxdqy.exceptions; 5 import com.google.common.collect.Maps; 6 import org.apache.log4j.Logger; 7 import org.springframework.beans.ConversionNotSupportedException; 8 import org.springframework.beans.TypeMismatchException; 9 import org.springframework.http.converter.HttpMessageNotReadableException; 10 import org.springframework.http.converter.HttpMessageNotWritableException; 11 import org.springframework.web.HttpMediaTypeNotAcceptableException; 12 import org.springframework.web.HttpRequestMethodNotSupportedException; 13 import org.springframework.web.bind.MissingServletRequestParameterException; 14 import org.springframework.web.bind.annotation.ControllerAdvice; 15 import org.springframework.web.bind.annotation.ExceptionHandler; 16 import org.springframework.web.bind.annotation.ResponseBody; 17 18 import java.io.IOException; 19 import java.util.Map; 20 21 /** 22 * @author : hao 23 * @project : daieweb 24 * @description : 25 * @time : 2018/5/30 18:25 26 */ 27 @ControllerAdvice 28 public class WebExceptionHandler { 29 30 private static Logger logger = Logger.getLogger(WebExceptionHandler.class); 31 32 33 private static Map<String, String> messageMap = Maps.newHashMap(); 34 35 static { 36 37 messageMap.put("runtimeException", "运行时异常"); 38 messageMap.put("nullPointerException", "空指针异常"); 39 messageMap.put("classCastException", "类型转换异常"); 40 messageMap.put("iOException", "IO异常"); 41 messageMap.put("noSuchMethodException", "未知方法异常"); 42 messageMap.put("indexOutOfBoundsException", "数组越界异常"); 43 messageMap.put("httpMessageNotReadableException", "参数格式错误或没有无参构造器"); 44 messageMap.put("typeMismatchException", "参数类型异常"); 45 messageMap.put("missingServletRequestParameterException", "缺少参数异常"); 46 messageMap.put("httpRequestMethodNotSupportedException", "请求类型异常"); 47 messageMap.put("httpMediaTypeNotAcceptableException", "请求后缀异常或MediaType前后不匹配"); 48 messageMap.put("conversionNotSupportedException", "类型注入可能非接口异常"); 49 messageMap.put("httpMessageNotWritableException", "结果转换异常可能存在bean属性为空"); 50 } 51 52 @ExceptionHandler(RuntimeException.class) 53 @ResponseBody 54 public ResponseResult<Object> runtimeExceptionHandler(RuntimeException runtimeException) { 55 logger.error(runtimeException.getMessage(), runtimeException); 56 return RestResultGenerator.genResult(messageMap.get("runtimeException") + ":" + runtimeException.getMessage()); 57 } 58 59 @ExceptionHandler(NullPointerException.class) 60 @ResponseBody 61 public ResponseResult<Object> nullPointerExceptionHandler(NullPointerException nullPointerException) { 62 logger.error(nullPointerException.getMessage(), nullPointerException); 63 return RestResultGenerator.genResult(messageMap.get("nullPointerException") + ":" + nullPointerException.getMessage()); 64 } 65 66 @ExceptionHandler(ClassCastException.class) 67 @ResponseBody 68 public ResponseResult<Object> classCastExceptionHandler(ClassCastException classCastException) { 69 logger.error(classCastException.getMessage(), classCastException); 70 return RestResultGenerator.genResult(messageMap.get("classCastException") + ":" + classCastException.getMessage()); 71 } 72 73 @ExceptionHandler(IOException.class) 74 @ResponseBody 75 public ResponseResult<Object> iOExceptionHandler(IOException iOException) { 76 logger.error(iOException.getMessage(), iOException); 77 return RestResultGenerator.genResult(messageMap.get("iOException") + ":" + iOException.getMessage()); 78 } 79 80 @ExceptionHandler(NoSuchMethodException.class) 81 @ResponseBody 82 public ResponseResult<Object> noSuchMethodExceptionHandler(NoSuchMethodException noSuchMethodException) { 83 logger.error(noSuchMethodException.getMessage(), noSuchMethodException); 84 return RestResultGenerator.genResult(messageMap.get("noSuchMethodException") + ":" + noSuchMethodException.getMessage()); 85 } 86 87 @ExceptionHandler(IndexOutOfBoundsException.class) 88 @ResponseBody 89 public ResponseResult<Object> indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException indexOutOfBoundsException) { 90 logger.error(indexOutOfBoundsException.getMessage(), indexOutOfBoundsException); 91 return RestResultGenerator.genResult(messageMap.get("indexOutOfBoundsException") + ":" + indexOutOfBoundsException.getMessage()); 92 } 93 94 @ExceptionHandler({HttpMessageNotReadableException.class}) 95 @ResponseBody 96 public ResponseResult<Object> requestNotReadable(HttpMessageNotReadableException httpMessageNotReadableException) { 97 logger.error(httpMessageNotReadableException.getMessage(), httpMessageNotReadableException); 98 return RestResultGenerator.genResult(messageMap.get("httpMessageNotReadableException") + ":" + httpMessageNotReadableException.getMessage()); 99 } 100 101 @ExceptionHandler({TypeMismatchException.class}) 102 @ResponseBody 103 public ResponseResult<Object> requestTypeMismatch(TypeMismatchException typeMismatchException) { 104 logger.error(typeMismatchException.getMessage(), typeMismatchException); 105 return RestResultGenerator.genResult(messageMap.get("typeMismatchException") + ":" + typeMismatchException.getMessage()); 106 } 107 108 @ExceptionHandler({MissingServletRequestParameterException.class}) 109 @ResponseBody 110 public ResponseResult<Object> requestMissingServletRequest(MissingServletRequestParameterException missingServletRequestParameterException) { 111 logger.error(missingServletRequestParameterException.getMessage(), missingServletRequestParameterException); 112 return RestResultGenerator.genResult(messageMap.get("missingServletRequestParameterException") + ":" + missingServletRequestParameterException.getMessage()); 113 } 114 115 @ExceptionHandler({HttpRequestMethodNotSupportedException.class}) 116 @ResponseBody 117 public ResponseResult<Object> request405(HttpRequestMethodNotSupportedException httpRequestMethodNotSupportedException) { 118 logger.error(httpRequestMethodNotSupportedException.getMessage(), httpRequestMethodNotSupportedException); 119 return RestResultGenerator.genResult(messageMap.get("httpRequestMethodNotSupportedException") + ":" + httpRequestMethodNotSupportedException.getMessage()); 120 } 121 122 @ExceptionHandler({HttpMediaTypeNotAcceptableException.class}) 123 @ResponseBody 124 public ResponseResult<Object> request406(HttpMediaTypeNotAcceptableException httpMediaTypeNotAcceptableException) { 125 logger.error(httpMediaTypeNotAcceptableException.getMessage(), httpMediaTypeNotAcceptableException); 126 return RestResultGenerator.genResult(messageMap.get("httpMediaTypeNotAcceptableException") + ":" + httpMediaTypeNotAcceptableException.getMessage()); 127 } 128 129 @ExceptionHandler({ConversionNotSupportedException.class}) 130 @ResponseBody 131 public ResponseResult<Object> server500(ConversionNotSupportedException conversionNotSupportedException) { 132 logger.error(conversionNotSupportedException.getMessage(), conversionNotSupportedException); 133 return RestResultGenerator.genResult(messageMap.get("conversionNotSupportedException") + ":" + conversionNotSupportedException.getMessage()); 134 } 135 136 137 @ExceptionHandler({HttpMessageNotWritableException.class}) 138 @ResponseBody 139 public ResponseResult<Object> server500(HttpMessageNotWritableException httpMessageNotWritableException) { 140 logger.error(httpMessageNotWritableException.getMessage(), httpMessageNotWritableException); 141 return RestResultGenerator.genResult(messageMap.get("conversionNotSupportedException") + ":" + httpMessageNotWritableException.getMessage()); 142 } 143 }
异常结果封装:
import java.util.List; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class RestResultGenerator { private static final Logger LOGGER = LoggerFactory.getLogger(RestResultGenerator.class); public RestResultGenerator() { } public static <T> ResponseResult<List<T>> genResult(PagerBean<T> data) { ResponseResult result = new ResponseResult(); result.setSuccess(true); result.setData(data.getData()); result.setTotal(data.getTotal()); result.setPageIndex(data.getPageIndex()); result.setPageSize(data.getPageSize()); return result; } public static <T> ResponseResult<T> genResult(T data, String message) { ResponseResult result = new ResponseResult(); result.setSuccess(true); result.setData(data); result.setMessage(message); return result; } public static <T> ResponseResult<T> genResult(String error) { ResponseResult result = new ResponseResult(); if(StringUtils.isEmpty(error)) { result.setSuccess(true); } else { result.setSuccess(false); } result.setError(error); return result; } }
返回结果bean:
@JsonInclude(Include.NON_EMPTY) //为空的属性不参与序列化 public class ResponseResult<T> { private boolean success = true; private String error; private T data; private String message; private long total; private int pageSize; private int pageIndex; public ResponseResult() { } public long getTotal() { return this.total; } public void setTotal(long total) { this.total = total; } public int getPageSize() { return this.pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPageIndex() { return this.pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public String getError() { return this.error; } public void setError(String error) { this.error = error; } public T getData() { return this.data; } public void setData(T data) { this.data = data; } public String getMessage() { return this.message; } public void setMessage(String message) { this.message = message; } public boolean isSuccess() { return this.success; } public void setSuccess(boolean success) { this.success = success; } }