其他分享
首页 > 其他分享> > 全局异常

全局异常

作者:互联网

package com.itheima.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

//全局异常处理返回数据
@RestControllerAdvice("com.itheima.controller")
public class GlobalExceptionHandler {
@ExceptionHandler
public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
String message = ex.getMessage();

if (message.contains("Duplicate entry")){
//切割第三个字母是输入的名字
String[] s = message.split(" ");
return R.error(s[2]+"已存在");

}
return R.error("未知错误");
}
@ExceptionHandler
public R<String> customExpectionHandler(CustomExpection ex){
return R.error(ex.getMessage());
}
}

标签:return,ExceptionHandler,public,ex,import,全局,异常,message
来源: https://www.cnblogs.com/pengkang/p/16500276.html