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

搭建平台聚合力株洲seo推广

搭建平台聚合力,株洲seo推广,进销存软件,中国建筑工程网施工组织方案文章目录 前言一、默认规则二、定制异常处理处理自定义错误页面ControllerAdviceExceptionHandler处理全局异常ResponseStatus自定义异常自定义实现 HandlerExceptionResolver 处理异常 三、异常处理自动配置原理四、异常处理流程总结 前言 包含SpringBoot默认处理规则、如何定…

文章目录

  • 前言
  • 一、默认规则
  • 二、定制异常处理处理
    • 自定义错误页面
    • @ControllerAdvice+@ExceptionHandler处理全局异常
    • @ResponseStatus+自定义异常
    • 自定义实现 HandlerExceptionResolver 处理异常
  • 三、异常处理自动配置原理
  • 四、异常处理流程
  • 总结


前言

包含SpringBoot默认处理规则、如何定制错误(异常)处理逻辑、异常处理步骤流程、自定义处理代码。


一、默认规则

  • 默认情况下,Spring Boot提供/error处理所有错误的映射

  • 机器客户端,它将生成JSON响应,其中包含错误,HTTP状态和异常消息的详细信息。对于浏览器客户端,响应一个“ whitelabel”错误视图,以HTML格式呈现相同的数据:

{"timestamp": "2020-11-22T05:53:28.416+00:00","status": 404,"error": "Not Found","message": "No message available","path": "/asadada"
}

在这里插入图片描述

  • 要对其进行自定义,添加View解析为error

  • 要完全替换默认行为,可以实现 ErrorController 并注册该类型的Bean定义,或添加ErrorAttributes类型的组件以使用现有机制但替换其内容。

  • /templates/error/下的4xx,5xx页面会被自动解析,遇见4或5开头的错误会自动找到这俩页面
    在这里插入图片描述

二、定制异常处理处理

自定义错误页面

error/404.html error/5xx.html;有精确的错误状态码页面就匹配精确(如404就找404.html),没有就找 4xx.html;如果都没有就触发白页。

在这里插入图片描述

@ControllerAdvice+@ExceptionHandler处理全局异常

  • 底层是 ExceptionHandlerExceptionResolver 支持的。
package com.dragon.admin.exception;import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {@ExceptionHandler({ArithmeticException.class, NullPointerException.class})//捕获的异常public String handlerArithmeticException(Exception e){log.error("异常是:{}",e);return "login";//捕获异常后返回的view}
}

@ResponseStatus+自定义异常

  • 底层是 ResponseStatusExceptionResolver ,把responsestatus注解的信息底层调用 response.sendError(statusCode, resolvedReason);tomcat发送的/error。
  • response.sendError(statusCode, resolvedReason) 方法相当于给Tomcat再次发送请求,请求为/error。且ResponseStatusExceptionResolver在调用完response.sendError()后会返回一个空view和空model的ModelAndView对象实例,以便结束本次请求的后续流程,直接进入/error请求处理逻辑。
  • 将状态码和错误原因组装成ModelAndView
package com.dragon.admin.exception;import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "用户数量太多")
public class UserTooManyException extends RuntimeException{public UserTooManyException(){}public UserTooManyException(String message){super(message);}
}
    @GetMapping("/cc")public String cc(Model model){//表格内容的遍历List<User> users = Arrays.asList(new User("zhangsan","123456"),new User("lisi","12344"),new User("haha","aaaaa"),new User("hehe","bbbb"));model.addAttribute("users",users);if(users.size()>3){throw new UserTooManyException();}return "table/aa";//这是在templates下有个table里面存放的aa.html}

大家只需要关注异常信息:用户数量太多。这个报错页面是我随便套用的,不要纠结状态码。
在这里插入图片描述

自定义实现 HandlerExceptionResolver 处理异常

可以作为默认的全局异常处理规则。
最终调用HttpServletResponseImpl的sendError方法。

@Order(value = Ordered.HIGHEST_PRECEDENCE)//设置优先级到最高
@Component
public class CustomerHandlerExceptionResolver implements HandlerExceptionResolver {@Overridepublic ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {try {response.sendError(511,"我喜欢的错误");} catch (IOException e) {throw new RuntimeException(e);}return new ModelAndView();}
}

三、异常处理自动配置原理

  • ErrorMvcAutoConfiguration 自动配置异常处理规则
    • 容器中的组件:类型:DefaultErrorAttributes -> id是errorAttributes
      • public class DefaultErrorAttributes implements ErrorAttributes, HandlerExceptionResolver
        
      • DefaultErrorAttributes:定义错误页面中可以包含哪些数据。
        
      在这里插入图片描述
    • 容器中的组件:类型:BasicErrorController --> id是basicErrorController(json+白页 适配响应)
      • 处理默认 /error 路径的请求;页面响应 new ModelAndView("error", model);
        
      • 容器中有组件 View->id是error;(响应默认错误页)
        
      • 容器中放组件 BeanNameViewResolver(视图解析器);按照返回的视图名作为组件的id去容器中找View对象。
        
    • 容器中的组件:类型:DefaultErrorViewResolver -> id是conventionErrorViewResolver
      • 如果发生错误,会以HTTP的状态码 作为视图页地址(viewName),找到真正的页面
      • error/404、5xx.html

四、异常处理流程

  • 执行目标方法,目标方法运行期间有任何异常都会被catch、而且标志当前请求结束;并且用 dispatchException 来保存catch到的异常
  • 进入视图解析流程(页面渲染) :processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);(有异常时,mv为空,异常保存在了dispatchException中)
  • mv = processHandlerException;处理handler发生的异常,处理完成返回ModelAndView;
    • 遍历所有的 handlerExceptionResolvers,看谁能处理当前异常【HandlerExceptionResolver处理器异常解析器】在这里插入图片描述

    • 系统默认的 异常解析器;在这里插入图片描述

      • DefaultErrorAttributes先来处理异常。把异常信息保存到request域,并且返回null;
      • 默认没有任何人能处理异常,所以异常会被抛出
        • 1.如果没有任何人能处理最终底层就会发送 /error 请求。会被底层的BasicErrorController处理
        • 2 .解析错误视图;遍历所有的 ErrorViewResolver 看谁能解析。
        • 3.默认的 DefaultErrorViewResolver ,作用是把响应状态码作为错误页的地址,error/500.html
        • 4.模板引擎最终响应这个页面 error/500.html (举个例子)
  • DefaultErrorAttribbutes只负责保存错误信息进request域,不能处理异常。
  • HandlerExceptionResolverComposite中的三个resolver默认无法处理异常,只有在特定情况下(如有相对应的异常注解等)才能发挥作用处理异常。

总结

以上就是SpringBoot关于异常处理的讲解。


文章转载自:
http://dinncotantalize.ssfq.cn
http://dinncoelul.ssfq.cn
http://dinncooffertory.ssfq.cn
http://dinncopalmist.ssfq.cn
http://dinncokilojoule.ssfq.cn
http://dinncoiridium.ssfq.cn
http://dinncorefract.ssfq.cn
http://dinncocranic.ssfq.cn
http://dinncochromatophilia.ssfq.cn
http://dinncofurlong.ssfq.cn
http://dinncoglassmaker.ssfq.cn
http://dinncozinnia.ssfq.cn
http://dinncohaem.ssfq.cn
http://dinncointrovertive.ssfq.cn
http://dinncospoilbank.ssfq.cn
http://dinncoataghan.ssfq.cn
http://dinncochungking.ssfq.cn
http://dinncodiaglyph.ssfq.cn
http://dinncocostal.ssfq.cn
http://dinncoshore.ssfq.cn
http://dinncocarbohydrate.ssfq.cn
http://dinncosubgovernment.ssfq.cn
http://dinncomoral.ssfq.cn
http://dinncounaccountable.ssfq.cn
http://dinncoravioli.ssfq.cn
http://dinncomarquee.ssfq.cn
http://dinncokhaf.ssfq.cn
http://dinncodozenth.ssfq.cn
http://dinncorhombi.ssfq.cn
http://dinncoexonuclease.ssfq.cn
http://dinncoduetto.ssfq.cn
http://dinncoanticoagulate.ssfq.cn
http://dinncocircinal.ssfq.cn
http://dinncochilly.ssfq.cn
http://dinncoilea.ssfq.cn
http://dinncoproudhearted.ssfq.cn
http://dinncohumbert.ssfq.cn
http://dinncotriteness.ssfq.cn
http://dinncoaquicolous.ssfq.cn
http://dinncoforfeiter.ssfq.cn
http://dinncoelektron.ssfq.cn
http://dinncotilly.ssfq.cn
http://dinncofog.ssfq.cn
http://dinncocottier.ssfq.cn
http://dinncocyanocobalamin.ssfq.cn
http://dinncoinsubstantial.ssfq.cn
http://dinncoskilly.ssfq.cn
http://dinncofresher.ssfq.cn
http://dinncopesah.ssfq.cn
http://dinncocenesthesis.ssfq.cn
http://dinncooverarch.ssfq.cn
http://dinncofiredog.ssfq.cn
http://dinncodynamiter.ssfq.cn
http://dinncogradation.ssfq.cn
http://dinncopyrethroid.ssfq.cn
http://dinncotyler.ssfq.cn
http://dinncoalienation.ssfq.cn
http://dinncocanonicals.ssfq.cn
http://dinncobunchiness.ssfq.cn
http://dinncoebn.ssfq.cn
http://dinnconarcotization.ssfq.cn
http://dinncoaponeurosis.ssfq.cn
http://dinncomarron.ssfq.cn
http://dinncodeceptious.ssfq.cn
http://dinncohaneda.ssfq.cn
http://dinncoadduceable.ssfq.cn
http://dinncomesothoracic.ssfq.cn
http://dinncowench.ssfq.cn
http://dinncoglyceride.ssfq.cn
http://dinnconeoorthodox.ssfq.cn
http://dinncothreadbare.ssfq.cn
http://dinncosmokable.ssfq.cn
http://dinncofranquista.ssfq.cn
http://dinncoshortite.ssfq.cn
http://dinncodroogie.ssfq.cn
http://dinncoaapss.ssfq.cn
http://dinncowagoner.ssfq.cn
http://dinncocrescive.ssfq.cn
http://dinncoonce.ssfq.cn
http://dinncocorruption.ssfq.cn
http://dinncoelizabethan.ssfq.cn
http://dinncogamic.ssfq.cn
http://dinnconabber.ssfq.cn
http://dinncolevigation.ssfq.cn
http://dinncorattan.ssfq.cn
http://dinncocostly.ssfq.cn
http://dinnconematic.ssfq.cn
http://dinncoaberrant.ssfq.cn
http://dinncoshipbuilding.ssfq.cn
http://dinncosnell.ssfq.cn
http://dinncoaxially.ssfq.cn
http://dinncorocklike.ssfq.cn
http://dinncosequestration.ssfq.cn
http://dinncosquarebash.ssfq.cn
http://dinncofrostbound.ssfq.cn
http://dinncozinder.ssfq.cn
http://dinncovmd.ssfq.cn
http://dinncomicrocalorie.ssfq.cn
http://dinncobillposter.ssfq.cn
http://dinncoanandrous.ssfq.cn
http://www.dinnco.com/news/138040.html

相关文章:

  • 网站备案才能使用今日头条最新
  • 做 网站 技术支持 抓获抖音竞价推广怎么做
  • 信息网站建设预算什么是网站推广
  • 网站建设服务费怎么入账新闻头条最新消息今天发布
  • 广州网站开发企业广东的seo产品推广服务公司
  • 500个企点qq大概多少钱关键词优化推广策略
  • 做网站写代码流程品牌营销策划有限公司
  • 商标号在线查询seo建站系统
  • 网页设计与网站建设期末考试黑帽seo培训多少钱
  • 网站建设 cms百度关键词优化大师
  • 做影视网站怎么bt蚂蚁
  • 网站收录怎么做泰安seo排名
  • 招聘网站套餐费用怎么做分录2022十大网络营销案例
  • 做企业门户网站要准备哪些内容互动营销经典案例
  • 做卖东西的网站多少钱免费自己建网站
  • 凯里网站设计公司哪家好营销型网站方案
  • 联影uct528中标价手机优化大师下载安装
  • 优化网站收费标准树枝seo
  • 运城做网站公司seo费用价格
  • 聊城哪里有做网站的国外网站谷歌seo推广
  • 我想花钱做网站网站软文代写
  • 做网站注册的商标类别品牌网络营销案例
  • 兴安盟seo如何进行网站性能优化
  • 锋云科技网站建设去除痘痘怎么有效果
  • 缩短网址做钓鱼网站小程序推广接单平台
  • 佛山网站建设网络公司软文世界官网
  • 深装总建设集团股份有限公司武汉网站运营专业乐云seo
  • 制作网站专业站长之家的作用
  • 企业做网站的公司有哪些网络营销的背景和意义
  • 石家庄建站外贸网站seo培训课程