A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

5.3.3 异常捕获类
使用 @ControllerAdvice和@ExceptionHandler注解来捕获指定类型的异常
[AppleScript] 纯文本查看 复制代码
package com.xuecheng.framework.exception;  
 import com.xuecheng.framework.model.response.ResponseResult; 
import com.xuecheng.framework.model.response.ResultCode; 
import org.slf4j.Logger;
 import org.slf4j.LoggerFactory; 
import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler; 
import org.springframework.web.bind.annotation.ResponseBody; 
    @ControllerAdvice public class ExceptionCatch { 
    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);   
    //捕获 CustomException异常  
   @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;   
  }
 }

5.3.4异常处理测试 5.3.4.1定义错误代码
每个业务操作的异常使用异常代码去标识。

[AppleScript] 纯文本查看 复制代码
package com.xuecheng.framework.domain.cms.response;  
  import com.xuecheng.framework.model.response.ResultCode;  import lombok.ToString; 
     @ToString  public enum CmsCode implements ResultCode {  
    CMS_ADDPAGE_EXISTS(false,24001,"页面已存在!");  
    //操作结果 
 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;  
    }  
}

5.3.4.2 异常处理测试
1、抛出异常 在controller、service、 dao中都可以抛出异常。
修改PageService的add方法,添加抛出异常的代码

[AppleScript] 纯文本查看 复制代码
/校验页面是否存在,根据页面名称、站点Id、页面webpath查询 
         CmsPage cmsPage1 =  cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(),  cmsPage.getSiteId(), cmsPage.getPageWebPath());    
      if(cmsPage1 !=null){     
          //校验页面是否存在,已存在则抛出异常     
         ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTS);   
       } 

2、启动工程,扫描到异常捕获的类ExceptionCatch
在springBoot的启动类中添加

[AppleScript] 纯文本查看 复制代码
 @ComponentScan(basePackages="com.xuecheng.framework")//扫描common工程下的类

3、前端展示异常信息
服务端响应信息如下:


页面提取异常处理

[AppleScript] 纯文本查看 复制代码
addSubmit(){ 
  this.$refs.pageForm.validate((valid) => {   
  if (valid) {   
    this.$confirm('确认提交吗?', '提示', {}).then(() => {    
     cmsApi.page_add(this.pageForm).then((res) => {  
           console.log(res);    
       if(res.success){       
      this.$message({      
         message: '提交成功',     
          type: 'success'      
       });         
    this.$refs['pageForm'].resetFields();   
        }else if(res.message){   
          this.$message.error(res.message);    
       }else{     
        this.$message.error('提交失败');  
         }      
   });     
  });   
  }  
 }); }



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马