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

wordpress站内优化中国建设银行安徽分行网站

wordpress站内优化,中国建设银行安徽分行网站,网站管理助手3.0,wordpress手机编辑器目录 一、 错误处理 1. 默认规则 2. 定制错误处理逻辑 二、自定义异常处理 1. 实现 ErrorController 2. RestControllerAdvice/ControllerAdvice ExceptionHandler 实现自定义异常 3. 新建 UserController.class 测试 3 种不同异常的处理 4. 最终效果如下 补充 1. 参…目录 一、 错误处理 1. 默认规则 2. 定制错误处理逻辑 二、自定义异常处理 1. 实现 ErrorController 2. RestControllerAdvice/ControllerAdvice ExceptionHandler 实现自定义异常 3. 新建 UserController.class 测试 3 种不同异常的处理 4. 最终效果如下 补充 1. 参数校验所需依赖以及使用方式 2. 追踪抛出错误的方法 一、 错误处理 1. 默认规则 1默认情况下SpringBoot 提供了 /error 处理所有错误的映射 2对于机器客户端它将生成 JSON 响应其中包含错误HTTP 状态和异常消息的详细信息对于浏览器客户端它将响应一个 whitelabel 错误视图以 HTML 格式呈现相同的数据 3要对其自定义添加 View 解析为 Error 4要完全替换默认行为可以实现 ErrorController 并注册该类型的 Bean 定义或添加 ErrorAttributes 类型组件以使用现有机制并替换其内容ErrorAttributes 中定义了返回哪些错误项 2. 定制错误处理逻辑 1自定义错误页面 error/404.htmlerror/500.html 放到 /resources/public/error 文件夹下即可也可以将这两个 html 命名为 4xx.html5xx.html 则可处理对应 4xx、5xx 错误当然还可以每个状态码放一个对应页面 2通过 ControllerAdvice/RestControllerAdvice  ExceptionHandler 处理异常 3实现 HandlerExceptionResolver 处理异常 二、自定义异常处理 1. 实现 ErrorController 1. 新建 GlobalErrorController.class 实现 ErrorController 接口并实现如下逻辑 RestController public class GlobalErrorController implements ErrorController {// 错误请求路径private final String ERROR_PATH /error;Resourceprivate ErrorAttributes errorAttributes;Overridepublic String getErrorPath() {return ERROR_PATH;}/*** JSON格式错误信息*/RequestMapping(value ERROR_PATH, produces {MediaType.APPLICATION_JSON_VALUE})public MapString, Object error(WebRequest webRequest) {ErrorAttributeOptions options ErrorAttributeOptions.of(ErrorAttributeOptions.Include.MESSAGE);MapString, Object body this.errorAttributes.getErrorAttributes(webRequest, options);return body;} } 注意 1ERROR_PATH /error 这个路径其实就是 SpringBoot 错误处理机制中自动配置的路径在 ErrorProperties.class 中可找到这个配置 2如果在 application.properties 覆盖了默认错误路径则上面代码中 ERROR_PATH 应设置为配置文件中的错误路径 # 全局错误映射路径 server.error.path /error 3这句代码ErrorAttributeOptions.of(ErrorAttributeOptions.Include.MESSAGE)的意思是返回给前端的错误信息中包含哪些信息这个 of() 方法中可以指定返回信息范围(可以指定多个)共有 4 个选项值 ErrorAttributeOptions.Include.EXCEPTIONInclude the exception class name attributeErrorAttributeOptions.Include.STACK_TRACEInclude the stack trace attributeErrorAttributeOptions.Include.MESSAGEInclude the message attributeErrorAttributeOptions.Include.BINDING_ERRORSInclude the binding errors attribute 不同值返回信息如下 a. ErrorAttributeOptions.Include.EXCEPTION {timestamp: 2022-08-07T13:53:40.60700:00,status: 500,error: Internal Server Error,exception: java.lang.RuntimeException,message: ,path: /error123 } b. ErrorAttributeOptions.Include.STACK_TRACE显示信息最多 {timestamp: 2022-08-07T13:54:14.10100:00,status: 500,error: Internal Server Error,trace: java.lang.RuntimeException: Error\r\n\tat com.study ... ...message: ,path: /error123 } c. ErrorAttributeOptions.Include.MESSAGE {timestamp: 2022-08-07T13:54:56.75100:00,status: 500,error: Internal Server Error,message: Error,path: /error123 } d. ErrorAttributeOptions.Include.BINDING_ERRORS {timestamp: 2022-08-07T13:53:03.79100:00,status: 500,error: Internal Server Error,message: ,path: /error123 } 2. RestControllerAdvice/ControllerAdvice ExceptionHandler 实现自定义异常 在一次请求调用过程中如果程序出现异常我们应对异常进行拦截把异常中重要信息记录在日志便于排查错误只提供简单的错误信息返回给前端这个时候通过 RestControllerAdvice/ControllerAdvice ExceptionHandler 可轻松实现 1. 新建 GlobalException.class 继承 RuntimeException 并重写构造方法 Data public class GlobalException extends RuntimeException {// 错误编码private Integer errorCode;// 错误信息private String errorMsg;// 具体信息private String errorInfo;public GlobalException(Integer errorCode, String errorMsg, String errorInfo) {super(errorMsg);this.errorCode errorCode;this.errorMsg errorMsg;this.errorInfo errorInfo;}public GlobalException(Integer errorCode, String errorMsg, Throwable cause) {super(errorMsg, cause);this.errorCode errorCode;this.errorMsg errorMsg;this.errorInfo cause.getMessage();} } 2. 新建 GlobalExceptionHandler.class 实现对系统错误的统一处理 RestControllerAdvice public class GlobalExceptionHandler {/*** 处理全局系统异常(非自定义异常)*/ExceptionHandler(Exception.class)public MapString, Object handleSystemException(Exception exception) {MapString, Object exceptionInfo new HashMap();exceptionInfo.put(errorCode, 500);exceptionInfo.put(errorMsg, 系统故障);// 日志记录具体信息不要返回给前端System.out.println(exception.getMessage());return exceptionInfo;}/*** 处理自定义异常*/ExceptionHandler(GlobalException.class)public MapString, Object handleGlobalException(GlobalException exception) {MapString, Object exceptionInfo new HashMap();exceptionInfo.put(errorCode, exception.getErrorCode());exceptionInfo.put(errorMsg, exception.getErrorMsg());// 日志记录具体信息不要返回给前端System.out.println(exception.getErrorInfo());return exceptionInfo;}/*** 实体类属性校验异常*/ ExceptionHandler(MethodArgumentNotValidException.class)public MapString, Object MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException exception) {ObjectError objectError exception.getBindingResult().getAllErrors().get(0);MapString, Object exceptionInfo new HashMap();exceptionInfo.put(errorCode, 550);exceptionInfo.put(errorMsg, objectError.getDefaultMessage());return exceptionInfo;} } 3. 新建 UserController.class 测试 3 种不同异常的处理 RestController public class UserController {/*** 测试全局异常*/GetMapping(/error1)public Integer error1() {int m 10 / 0;return m;}/*** 测试自定义异常*/GetMapping(/error2)public Integer error2() {try {int m 10 / 0;return m;} catch (Exception e) {throw new GlobalException(501, 系统错误, e);}}/*** 测试实体类字段校验异常*/PostMapping(/error3)public User error3(RequestBody Valid User user) {return user;} } 4. 最终效果如下 1全局异常测试(非自定义异常)http://localhost:8080/error1 {errorCode: 500,errorMsg: 系统故障 } 2自定义异常测试http://localhost:8080/error2 {errorCode: 501,errorMsg: 系统错误 } 3实体类字段校验异常测试http://localhost:8080/error3 {errorCode: 550,errorMsg: 年龄不能超过100岁 } 补充 1. 参数校验所需依赖以及使用方式 1实体类字段校验需要引入如下依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-validation/artifactId /dependency 2实体类上添加注解 Data public class User {NotBlank(message 用户名不能为空)private String name;NotNull(message 年龄不能为空)Min(value 1, message 年龄不能小于1岁)Max(value 100, message 年龄不能超过100岁)private Integer age; } 2. 追踪抛出错误的方法 上面的异常处理虽然把返回给前端的异常提示信息和日志记录的异常信息进行了处理但对于后端来说日志仅仅打印了错误信息并没有记录是哪个类哪个方法的抛出的异常这不便于后端调试所以可添加如下方法记录抛出错误的类和方法 1添加根据异常查找出执行的类和方法 /*** 找出执行类和执行方法*/ private String getExecutedMethod(Exception e) {StackTraceElement[] elements e.getStackTrace();if(elements.length 0) {// 异常链中第一个也就是最外层的信息, 当然就是 controller 这一层StackTraceElement target elements[0];return String.format(%s#%s, target.getClassName(), target.getMethodName());}return ; } 2在异常处理器中做如下修改即可 /*** 处理全局系统异常(非自定义异常)*/ ExceptionHandler(Exception.class) public MapString, Object handleSystemException(Exception exception) {MapString, Object exceptionInfo new HashMap();exceptionInfo.put(errorCode, 500);exceptionInfo.put(errorMsg, 系统故障);// 日志记录具体信息不要返回给前端String executedMethod getExecutedMethod(exception);String exceptionMessage String.format(执行方法: %s, 错误信息: %s, executedMethod, exception.getMessage());tem.out.println(exceptionMessage);return exceptionInfo; }
http://www.w-s-a.com/news/297015/

相关文章:

  • 以个人名义做地方门户网站社保服务个人网站
  • 上海企业做网站设计制作感悟150字
  • asp.netmvc网站开发ps设计网页
  • win2008 挂网站 404官方网站是什么
  • 网站只做内容 不做外链做姓氏图的网站
  • 中国建设银行信用卡黑名单网站wordpress怎么解密密码
  • 建设银行如何网站设置密码广州网站营销推广
  • 企业做网站的步骤与做网站注意事项四川省住房建设厅网站打不开
  • 网页设计网站规划报告百度文库官网登录入口
  • 郑州医疗网站开发wordpress能注册
  • 创建网站的英语石家庄微信网站建设
  • 分享几个x站好用的关键词微信商城小程序开发一般需要多少钱
  • 做韩国外贸网站wordpress手机版中文
  • 建站群赚钱有前途吗蚌埠北京网站建设
  • 北京网站建设求职简历十堰seo优化教程
  • 网站顶部可关闭广告微信小程序多少钱
  • 网站背景怎么弄斜杠青年seo工作室
  • ps个人网站首页怎么制作如何做网站的版块规划
  • 做网站的市场开源建站工具
  • 邹平做网站哪家好自动点击器app
  • 南阳seo网站排名优化wordpress文章对游客不显示
  • 网站301什么意思湛江市seo网站设计报价
  • 免费建造网站化妆品网络营销方案
  • 建公司网站wordpress the content
  • 网站的站点的管理系统建设银行网站注册企业
  • 长春火车站是哪个站做微商哪个网站有客源
  • 亚马逊培训费用一般多少seo专业培训课程
  • 做推文封面图网站南宁高端网站建设
  • 天津网站搜索排名做电影免费ppt模板下载网站
  • 襄樊最好网站建设价格网站建设与设计 毕业设计