添加异常mapping

This commit is contained in:
wangkang
2017-07-29 10:18:35 +08:00
parent 085c22f04e
commit 836a4c783e
2 changed files with 19 additions and 0 deletions
@@ -29,6 +29,10 @@ public enum ErrorCode {
* 描述 : 方法不允许
*/
METHOD_NOT_ALLOWED("method not allowed"),
/**
* 描述 : Http媒体类型不可接受
*/
HTTP_MEDIA_TYPE_NOT_ACCEPTABLE("Http Media Type Not Acceptable"),
/**
* 描述 : 资源不存在
*/
@@ -13,6 +13,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.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -138,6 +139,20 @@ public class ExceptionMapping {
return new RestResponse<>(ErrorCode.METHOD_NOT_ALLOWED, buildError(exception));
}
/**
* 描述 : 捕获HttpMediaTypeNotAcceptableException异常(Http媒体类型不可接受)
*
* @param exception 异常
* @return 错误信息
*/
@ExceptionHandler(value = HttpMediaTypeNotAcceptableException.class)
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
@ResponseBody
public RestResponse<String> httpMediaTypeNotAcceptableException(
HttpMediaTypeNotAcceptableException exception) {
return new RestResponse<>(ErrorCode.HTTP_MEDIA_TYPE_NOT_ACCEPTABLE, buildError(exception));
}
/**
* 描述 : 捕获SystemException异常(系统检查型异常)
*