添加异常处理方法

This commit is contained in:
Administrator
2017-07-29 10:18:34 +08:00
committed by wangkang
parent b99ae30c03
commit ebdfc9d914
2 changed files with 25 additions and 0 deletions
@@ -25,6 +25,10 @@ public enum ErrorCode {
* 描述 : 参数校验错误
*/
PARAMETER_VALID_ERROR("parameter check error"),
/**
* 描述 : 方法不允许
*/
METHOD_NOT_ALLOWED("method not allowed"),
/**
* 描述 : 资源不存在
*/
@@ -14,6 +14,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -167,6 +168,26 @@ public class ExceptionMapping {
return new RestResponse<>(returnCode, returnMessage, em);
}
/**
* 描述 : 捕获httpRequestMethodNotSupportedException异常(方法不允许)
*
* @param httpRequestMethodNotSupportedException 异常
* @return 错误信息
*/
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ResponseBody
public RestResponse<ErrorMessage> httpRequestMethodNotSupportedException(
HttpRequestMethodNotSupportedException httpRequestMethodNotSupportedException) {
String returnCode = ErrorCode.METHOD_NOT_ALLOWED.name();
String returnMessage = ErrorCode.METHOD_NOT_ALLOWED.value();
ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.METHOD_NOT_ALLOWED.value(),
returnCode, returnMessage, applicationConfig.isOutputExceptionStackTrace(),
httpRequestMethodNotSupportedException);
LOGGER.error(em.getId(), httpRequestMethodNotSupportedException);
return new RestResponse<>(returnCode, returnMessage, em);
}
/**
* 描述 : 捕获SystemException异常(系统检查型异常)
*