当前位置: 首页 > news >正文

建设网站外包公司培训心得体会

建设网站外包公司,培训心得体会,新平台推广赚钱,二级域名网站可以做关键词优化吗前言 软件开发springboot项目过程中,不可避免的需要处理各种异常,spring mvc 架构中各层会出现大量的try {...} catch {...} finally {...}代码块,不仅有大量的冗余代码,而且还影响代码的可读性。这样就需要定义个全局统一异常处理器&#x…

前言

软件开发springboot项目过程中,不可避免的需要处理各种异常,spring mvc 架构中各层会出现大量的try {...} catch {...} finally {...}代码块,不仅有大量的冗余代码,而且还影响代码的可读性。这样就需要定义个全局统一异常处理器,以便业务层再也不必处理异常。

推荐理由

  • 代码复制到项目中通过简单的配置即可实现

  • 可以灵活的根据自己的业务异常进行更细粒度的扩展

实践

1 封装统一返回结果类

源代码

public class AjaxResult {//是否成功private Boolean success;//状态码private Integer code;//提示信息private String msg;//数据private Object data;public AjaxResult() {}//自定义返回结果的构造方法public AjaxResult(Boolean success,Integer code, String msg,Object data) {this.success = success;this.code = code;this.msg = msg;this.data = data;}//自定义异常返回的结果public static AjaxResult defineError(BusinessException de){AjaxResult result = new AjaxResult();result.setSuccess(false);result.setCode(de.getErrorCode());result.setMsg(de.getErrorMsg());result.setData(null);return result;}//其他异常处理方法返回的结果public static AjaxResult otherError(ErrorEnum errorEnum){AjaxResult result = new AjaxResult();result.setMsg(errorEnum.getErrorMsg());result.setCode(errorEnum.getErrorCode());result.setSuccess(false);result.setData(null);return result;}public Boolean getSuccess() {return success;}public void setSuccess(Boolean success) {this.success = success;}public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}}

2 自定义异常封装类

源码:

public class BusinessException extends RuntimeException {private static final long serialVersionUID = 1L;/*** 错误状态码*/protected Integer errorCode;/*** 错误提示*/protected String errorMsg;public BusinessException(){}public BusinessException(Integer errorCode, String errorMsg) {this.errorCode = errorCode;this.errorMsg = errorMsg;}public Integer getErrorCode() {return errorCode;}public void setErrorCode(Integer errorCode) {this.errorCode = errorCode;}public String getErrorMsg() {return errorMsg;}public void setErrorMsg(String errorMsg) {this.errorMsg = errorMsg;}
}

3 错误枚举,拒绝硬编码

图片

源码

public enum ErrorEnum {// 数据操作错误定义SUCCESS(200, "成功"),NO_PERMISSION(403,"你没得权限"),NO_AUTH(401,"未登录"),NOT_FOUND(404, "未找到该资源!"),INTERNAL_SERVER_ERROR(500, "服务器异常请联系管理员"),;/** 错误码 */private Integer errorCode;/** 错误信息 */private String errorMsg;ErrorEnum(Integer errorCode, String errorMsg) {this.errorCode = errorCode;this.errorMsg = errorMsg;}public Integer getErrorCode() {return errorCode;}public String getErrorMsg() {return errorMsg;}
}

4 全局异常处理类

图片

源码

/*** 全局异常处理器* */
@RestControllerAdvice
public class GlobalExceptionHandler
{private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);/*** 处理自定义异常**/@ExceptionHandler(value = BusinessException.class)public AjaxResult bizExceptionHandler(BusinessException e) {log.error(e.getMessage(), e);return AjaxResult.defineError(e);}/***处理其他异常**/@ExceptionHandler(value = Exception.class)public AjaxResult exceptionHandler( Exception e) {log.error(e.getMessage(), e);return AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);}
}

5 测试

图片

返回结果:

http://www.hkea.cn/news/523933/

相关文章:

  • 邯郸教育行业网站建设百度推广代理商查询
  • 政府网站有哪些网站seo最新优化方法
  • 做广告牌子seo外链工具
  • 微信页面设计网站兰州网络推广技术
  • 上门做网站搜狗站长工具
  • wordpress用户邮箱验证码百度seo搜索引擎优化培训
  • 360极速怎么屏蔽网站新闻热点大事件
  • 购物app开发价格表站长工具seo排名
  • 微餐饮网站建设营销型网站建设方案
  • 高端网站建设公司好不好2020国内搜索引擎排行榜
  • 网站建设服务公司选哪家比较好?苏州优化收费
  • 中国建设银行河南省分行网站推广信息哪个平台好
  • 网站建设官网免费模板杭州seo优化
  • 绍兴网站建设谷歌搜索引擎在线
  • 网站的会员认证怎么做黑龙江新闻头条最新消息
  • 做网站如何分工百度推广登录平台客服
  • 网站建设如何提案万网域名注册信息查询
  • 创意二维码制作网站企业网络营销推广案例
  • 论坛型网站怎么做百度高级检索入口
  • 做百度移动网站排搜素引擎优化
  • 公司创建一个网站需要多少钱想做百度推广找谁
  • 做文献ppt模板下载网站有哪些常德政府网站
  • 青岛网站建设公司排行外链工具在线
  • 网站怎么做显得简洁美观seo数据是什么意思
  • 阿里巴巴开通诚信通后网站怎么做网络优化网站
  • 东莞手机网站价格便宜个人免费建站软件
  • 电子商务网站建设的步骤一般为百度100%秒收录
  • 做企业网站怎么样免费的推广软件下载
  • 拓普网站建设美国搜索引擎
  • 网站开发者工资冯耀宗seo视频教程