SpringMvc全局异常处理

@ControllerAdvice + @ExceptionHandler全局处理异常:
@ControllerAdvice,是spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强。
1、异常处理类,添加控制器增强注解,

@ControllerAdvice

public class RestExceptionHandler

// 运行时异常

@ExceptionHandler(RuntimeException.class)

@ResponseBody

public String runtimeExceptionHandler(RuntimeException runtimeException) {



    runtimeException.printStackTrace();

    return JsonUtil.toErrorCodeStr(ErrorCode.runtimeException);

}



// 空指针异常

@ExceptionHandler(NullPointerException.class)

@ResponseBody

public String nullPointerExceptionHandler(NullPointerException ex) {

    ex.printStackTrace();

    return JsonUtil.toErrorCodeStr(ErrorCode.nullPointerException);

}



// 类型转换异常

@ExceptionHandler(ClassCastException.class)

@ResponseBody

public String classCastExceptionHandler(ClassCastException ex) {

    ex.printStackTrace();

    return JsonUtil.toErrorCodeStr(ErrorCode.classCastException);

}



// IO异常

@ExceptionHandler(IOException.class)

@ResponseBody

public String iOExceptionHandler(IOException ex) {

    ex.printStackTrace();

    return JsonUtil.toErrorCodeStr(ErrorCode.iOException);

}



// 未知方法异常

@ExceptionHandler(NoSuchMethodException.class)

@ResponseBody

public String noSuchMethodExceptionHandler(NoSuchMethodException ex) {

    ex.printStackTrace();

    return JsonUtil.toErrorCodeStr(ErrorCode.noSuchMethodException);

}



// 400错误

@ExceptionHandler({ HttpMessageNotReadableException.class })

@ResponseBody

public String requestNotReadable(HttpMessageNotReadableException ex) {

    System.out.println("HttpMessageNotReadableException");

    return JsonUtil.toErrorCodeStr(ErrorCode.requestParamsException);

}



// 400错误

@ExceptionHandler({ TypeMismatchException.class })

@ResponseBody

public String requestTypeMismatch(TypeMismatchException ex) {

    System.out.println("TypeMismatchException");

    return JsonUtil.toErrorCodeStr(ErrorCode.requestParamsException);

}



// 400错误

@ExceptionHandler({ MissingServletRequestParameterException.class })

@ResponseBody

public String requestMissingServletRequest(MissingServletRequestParameterException ex) {

    System.out.println("MissingServletRequestParameterException");

    ex.printStackTrace();

    return JsonUtil.toErrorCodeStr(ErrorCode.requestParamsException);

}



// 404 有问题

@ExceptionHandler({ NoHandlerFoundException.class })

@ResponseBody

public String requestMissingServletRequest(NoHandlerFoundException ex) {

    System.out.println("NoHandlerFoundException");

    ex.printStackTrace();

    return JsonUtil.toErrorCodeStr(ErrorCode.noSuchRequestHandlingMethodException);

}



// 500错误

@ExceptionHandler({ ConversionNotSupportedException.class, HttpMessageNotWritableException.class })

@ResponseBody

public String server500(RuntimeException runtimeException) {

    return JsonUtil.toErrorCodeStr(ErrorCode.internalServerError);

}



// 自定义异常

@ExceptionHandler({ MyException.class })

@ResponseBody

public String MyException(MyException ex) {

    System.out.println(JsonUtil.toErrorCodeStr(ex.getErrorCode()));

    return JsonUtil.toErrorCodeStr(ex.getErrorCode());

}

2、自定义异常类,继承Exception

public class MyException extends Exception {

/**

 * 

 */

private static final long serialVersionUID = 1L;

private ErrorCode errorCode;



public MyException() {

    super();

}



public MyException(ErrorCode errorCode) {

    super();

    this.errorCode = errorCode;

}



public ErrorCode getErrorCode() {

    return errorCode;

}



public void setErrorCode(ErrorCode errorCode) {

    this.errorCode = errorCode;

}

}

3、错误代码类
public enum ErrorCode {

success(0, "Success", "成功"), 

failure(1, "Failure", "失败"), 

targetNotExist(2, "Target does not exist or have been deleted", "数据不存在或已被删除"),

userNameOrPasswdError(3, "UserName Or Passwd Error", "用户名或密码错误"), 

pleaseLogin(4, "Please Login", "请登录"),

oldPasswdError(5, "Old Passwd Error", "原密码错误"),

appAlreadyError(6, "App Already Error", "应用信息已存在配置"),

requestParamsException(400,"Request Params Exception","请求参数异常"),

noSuchRequestHandlingMethodException(404,"NoSuchRequestHandlingMethodException","API不存在"),

internalServerError(500,"Internal Server Error","服务器错误"),

runtimeException(1000, "RuntimeException", "[服务器]运行时异常"), 

nullPointerException(1001, "NullPointerException", "空指针异常"),

classCastException(1002,"ClassCastException","类型转换异常"),

iOException(1003,"IOException","IO异常"),

noSuchMethodException(999,"NoSuchMethodException","未知错误");



private int code;

private String description;

private String reason;



private ErrorCode(int code, String description, String reason) {

    this.code = code;

    this.description = description;

    this.reason = reason;

}



public int getCode() {

    return code;

}



public String getDescription() {

    return description;

}



public String getReason() {

    return reason;

}



public void setCode(int code) {

    this.code = code;

}



public void setDescription(String description) {

    this.description = description;

}



public void setReason(String reason) {

    this.reason = reason;

}

}

自定义抛出异常用法:

throw new MyException(ErrorCode.oldPasswdError)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值