项目层级
CommonError
1 package com.miaoshaProject.error; 2 3 /** 4 * @Author wangshuo 5 * @Date 2022/4/14, 8:39 6 * Please add a comment 7 */ 8 public interface CommonError { 9 10 public int getErrorCode(); 11 12 public String getErrorMsg(); 13 14 public CommonError setErrorMsg(String msg); 15 }
BusinessException
1 package com.miaoshaProject.error; 2 3 /** 4 * @Author wangshuo 5 * @Date 2022/4/14, 8:55 6 * 包装器业务异常类实现 7 */ 8 public class BusinessException extends Exception implements CommonError{ 9 10 //强关联一个CommonError 11 private CommonError commonError; 12 13 //直接接受CommonError的传参用于构造业务异常 14 public BusinessException(CommonError commonError){ 15 16 super(); 17 this.commonError = commonError; 18 } 19 20 //接收自定义异常传参用于构造自定义异常 21 public BusinessException(CommonError commonError,String msg){ 22 23 super(); 24 this.commonError = commonError; 25 this.commonError.setErrorMsg(msg); 26 } 27 28 @Override 29 public int getErrorCode() { 30 return commonError.getErrorCode(); 31 } 32 33 @Override 34 public String getErrorMsg() { 35 return commonError.getErrorMsg(); 36 } 37 38 @Override 39 public CommonError setErrorMsg(String msg) { 40 commonError.setErrorMsg(msg); 41 return this; 42 } 43 }
EnumBusinessError
1 package com.miaoshaProject.error; 2 3 /** 4 * @Author wangshuo 5 * @Date 2022/4/14, 8:43 6 * 自定义error 7 */ 8 public enum EnumBusinessError implements CommonError{ 9 //10001 参数不合法 10 PARAMETER_VALIDATION_ERROR(10001,"参数不合法"), 11 //20000未知错误 12 UNKNOWN_ERROR(20000,"未知错误"), 13 //以30000开头的错误码代表用户信息错误 14 USER_NOT_EXISTS(30001,"用户不存在") 15 ; 16 17 private EnumBusinessError(Integer code,String msg){ 18 19 this.errorCode = code; 20 this.errorMsg = msg; 21 } 22 23 private int errorCode; 24 private String errorMsg; 25 26 @Override 27 public int getErrorCode() { 28 return this.errorCode; 29 } 30 31 @Override 32 public String getErrorMsg() { 33 return this.errorMsg; 34 } 35 36 //定制化的方法改动错误信息 37 @Override 38 public CommonError setErrorMsg(String msg) { 39 this.errorMsg = msg; 40 return this; 41 } 42 }
getUser
1 @RequestMapping("/get") 2 @ResponseBody 3 public CommonReturnType getUser(@RequestParam(name = "id") Integer id) throws BusinessException { 4 5 //调用service服务获取对应id的用户对象并返回给前端 6 UserModel userModel = uesrService.getById(id); 7 8 if (userModel == null) { 9 //返回用户不存在异常 10 throw new BusinessException(EnumBusinessError.USER_NOT_EXISTS); 11 //空指针异常 12 // userModel.setAge(1514); 13 } 14 //将核心领域模型对象转化为可供UI使用的viewObject 15 //返回通用对象 16 return CommonReturnType.create(convertFromModel(userModel)); 17 }
BaseController
1 package com.miaoshaProject.controller; 2 3 import com.miaoshaProject.error.BusinessException; 4 import com.miaoshaProject.error.EnumBusinessError; 5 import com.miaoshaProject.response.CommonReturnType; 6 import org.springframework.http.HttpStatus; 7 import org.springframework.web.bind.annotation.ExceptionHandler; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 import org.springframework.web.bind.annotation.ResponseStatus; 10 11 import javax.servlet.http.HttpServletRequest; 12 import java.util.HashMap; 13 14 /** 15 * @Author wangshuo 16 * @Date 2022/4/14, 11:15 17 * 返回通用错误信息 18 */ 19 public class BaseController { 20 21 //定义通用的exceptionHandler解决未被Controller吸收的exception 22 @ExceptionHandler(Exception.class) 23 @ResponseStatus(HttpStatus.OK) 24 @ResponseBody 25 public Object handlerException(HttpServletRequest httpServletRequest, Exception e) { 26 27 HashMap<Object, Object> hashMap = new HashMap<>(); 28 if (e instanceof BusinessException) { 29 BusinessException businessException = (BusinessException) e; 30 hashMap.put("errCode", businessException.getErrorCode()); 31 hashMap.put("data", businessException.getErrorMsg()); 32 } else { 33 hashMap.put("errCode", EnumBusinessError.UNKNOWN_ERROR.getErrorCode()); 34 hashMap.put("data", EnumBusinessError.UNKNOWN_ERROR.getErrorMsg()); 35 } 36 return CommonReturnType.create(hashMap, "fail"); 37 } 38 }
本文来自博客园,作者:荣慕平,转载请注明原文链接:https://www.cnblogs.com/rongmuping/articles/16143856.html