系统对异常的处理使用统一的异常处理流程:1、自定义异常类型。2、自定义错误代码及错误信息。3、对于可预知的异常由程序员在代码中主动抛出,由SpringMVC统一捕获。 可预知异常是程序员在代码中手动抛出本系统定义的特定异常类型,由于是程序员抛出的异常,通常异常信息比较齐全,程序员在抛出时会指定错误代码及错误信息,获取异常信息也比较方便。4、对于不可预知的异常(运行时异常)由SpringMVC统一捕获Exception类型的异常。 不可预知异常通常是由于系统出现bug、或一些不要抗拒的错误(比如网络中断、服务器宕机等),异常类型为RuntimeException类型(运行时异常)。5、可预知的异常及不可预知的运行时异常最终会采用统一的信息格式(错误代码+错误信息)来表示,最终也会随请求响应给客户端。1、在controller、service、dao中程序员抛出自定义异常;springMVC框架抛出框架异常类型2、统一由异常捕获类捕获异常,并进行处理3、捕获到自定义异常则直接取出错误代码及错误信息,响应给用户。4、捕获到非自定义异常类型首先从Map中找该异常类型是否对应具体的错误代码,如果有则取出错误代码和错误信息并响应给用户,如果从Map中找不到异常类型所对应的错误代码则统一为99999错误代码并响应给用户。5、将错误代码及错误信息以Json格式响应给用户。错误处理实战在共有项目中common中建exception包专门来存错误处理类.CustomExceptionpublic class CustomException extends RuntimeException { private ResultCode resultCode; public CustomException(ResultCode resultCode){ //异常信息 错误代码+异常信息 super("错误代码:"+resultCode.code()+"错误信息:"+resultCode.message()); this.resultCode=resultCode; } public ResultCode getResultCode(){ return this.resultCode; }}其中作为响应的结果的ResultCode接口为 定义了基础信息:public interface ResultCode { //操作是否成功,true为成功,false操作失败 boolean success(); //操作代码 int code(); //提示信息 String message();}ExceptionCastpublic class ExceptionCast { //使用静态方法抛出自定义异常 public static void cast(ResultCode resultCode){ throw new CustomException(resultCode); }}ExceptionCatch@ControllerAdvicepublic class ExceptionCatch { private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class); //使用EXCEPTIONS存放异常类型和错误代码的映射,ImmutableMap的特点的一旦创建不可改变,并且线程安全 private static ImmutableMap<Class<? extends Throwable>,ResultCode> EXCEPTIONS; //使用builder来构建一个异常类型和错误代码的异常 protected static ImmutableMap.Builder<Class<? extends Throwable>,ResultCode> builder = ImmutableMap.builder(); @ExceptionHandler(Exception.class) @ResponseBody public ResponseResult exception(Exception e){ LOGGER.error("catch exception : {}\r\nexception: ",e.getMessage(), e); if(EXCEPTIONS == null) EXCEPTIONS = builder.build(); ResultCode resultCode = EXCEPTIONS.get(e.getClass()); ResponseResult responseResult; if (resultCode != null) { responseResult = new ResponseResult(resultCode); } else { responseResult = new ResponseResult(CommonCode.SERVER_ERROR); } return responseResult; } //捕获CustomEcception异常 @ExceptionHandler(CustomException.class) @ResponseBody public ResponseResult customException(CustomException e) { LOGGER.error("catch exception : {}\\r\\nexception: ", e.getMessage(), e); ResultCode resultCode = e.getResultCode(); ResponseResult responseResult = new ResponseResult(resultCode); return responseResult; } static{ //在这里加入一些基础的异常类型判断 builder.put(HttpMessageNotReadableException.class,CommonCode.INVALID_PARAM); }}CommonCodeimport lombok.ToString;@ToStringpublic enum CommonCode implements ResultCode{ INVALID_PARAM(false,10003,"非法参数!"), SUCCESS(true,10000,"操作成功!"), FAIL(false,11111,"操作失败!"), UNAUTHENTICATED(false,10001,"此操作需要登陆系统!"), UNAUTHORISE(false,10002,"权限不足,无权操作!"), SERVER_ERROR(false,99999,"抱歉,系统繁忙,请稍后重试!");// private static ImmutableMap<Integer, CommonCode> codes ; //操作是否成功 boolean success; //操作代码 int code; //提示信息 String message; private CommonCode(boolean success,int code, String message){ this.success = success; this.code = code; this.message = message; } @Override public boolean success() { return success; } @Override public int code() { return code; } @Override public String message() { return message; }}CmsCodeimport lombok.ToString;@ToStringpublic enum CmsCode implements ResultCode { CMS_ADDPAGE_EXISTSNAME(false,24001,"页面名称已存在!"), CMS_GENERATEHTML_DATAURLISNULL(false,24002,"从页面信息中找不到获取数据的url!"), CMS_GENERATEHTML_DATAISNULL(false,24003,"根据页面的数据url获取不到数据!"), CMS_GENERATEHTML_TEMPLATEISNULL(false,24004,"页面模板为空!"), CMS_GENERATEHTML_HTMLISNULL(false,24005,"生成的静态html为空!"), CMS_GENERATEHTML_SAVEHTMLERROR(false,24006,"保存静态html出错!"), CMS_COURSE_PERVIEWISNULL(false,24007,"预览页面为空!"); //操作代码 boolean success; //操作代码 int code; //提示信息 String message; private CmsCode(boolean success, int code, String message){ this.success = success; this.code = code; this.message = message; } @Override public boolean success() { return success; } @Override public int code() { return code; } @Override public String message() { return message; }}服务层add方法添加错误处理public CmsPageResult add(CmsPage cmsPage) { if (cmsPage==null) ExceptionCast.cast(CommonCode.INVALID_PARAM);//抛出通用错误类中的无效参数 //检验页面是否存在 根据页面名称 站点id 页面path查询 CmsPage cmsPage1 = cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(), cmsPage.getSiteId(), cmsPage.getPageWebPath()); /*if (cmsPage1 == null) { cmsPage.setPageId(null);//添加页面主键由spring data自动生成 cmsPageRepository.save(cmsPage); //返回结果 CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS, cmsPage); return cmsPageResult; }else { //如果不等于null就是有页面 添加失败抛出已存在页面异常 ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTSNAME); } return new CmsPageResult(CommonCode.FAIL, null);//如果存在则返回null 失败信息*/ //先列出所有错误再执行正确执行结果 if (cmsPage1!=null){ ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTSNAME); } cmsPage.setPageId(null);//添加页面主键由spring data自动生成 cmsPageRepository.save(cmsPage); //返回结果 return new CmsPageResult(CommonCode.SUCCESS, cmsPage);}注意:启动类上要加上扫描异常处理类所在位置的包,例如:@ComponentScan(basePackages={"com.xuecheng.framework"})