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

seo网站建设接单新媒体运营主要做什么

seo网站建设接单,新媒体运营主要做什么,编程培训班学费找极客时间,小程序app备案文章目录 前言一、默认规则二、定制异常处理处理自定义错误页面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://rippling.tgcw.cn
http://squareflipper.tgcw.cn
http://chaffinch.tgcw.cn
http://subtenant.tgcw.cn
http://kirundi.tgcw.cn
http://notionate.tgcw.cn
http://meagre.tgcw.cn
http://succeed.tgcw.cn
http://sexless.tgcw.cn
http://slut.tgcw.cn
http://tahina.tgcw.cn
http://catbird.tgcw.cn
http://kaf.tgcw.cn
http://chockstone.tgcw.cn
http://whoopla.tgcw.cn
http://wheelman.tgcw.cn
http://dingbat.tgcw.cn
http://consciousness.tgcw.cn
http://platycephalous.tgcw.cn
http://perissodactyla.tgcw.cn
http://heliotropic.tgcw.cn
http://flowered.tgcw.cn
http://laevorotation.tgcw.cn
http://marcot.tgcw.cn
http://coachfellow.tgcw.cn
http://probabilism.tgcw.cn
http://geryon.tgcw.cn
http://euhemerist.tgcw.cn
http://ahermatype.tgcw.cn
http://majordomo.tgcw.cn
http://astronautical.tgcw.cn
http://ishtar.tgcw.cn
http://jazzily.tgcw.cn
http://frugivorous.tgcw.cn
http://nominal.tgcw.cn
http://plf.tgcw.cn
http://fur.tgcw.cn
http://morwong.tgcw.cn
http://serrulate.tgcw.cn
http://lycopod.tgcw.cn
http://acclimatise.tgcw.cn
http://stiffness.tgcw.cn
http://relend.tgcw.cn
http://cityfied.tgcw.cn
http://univalvular.tgcw.cn
http://getable.tgcw.cn
http://melilla.tgcw.cn
http://metastases.tgcw.cn
http://squanderer.tgcw.cn
http://finlandization.tgcw.cn
http://hallow.tgcw.cn
http://lubavitcher.tgcw.cn
http://augustly.tgcw.cn
http://nuts.tgcw.cn
http://tenfold.tgcw.cn
http://southron.tgcw.cn
http://ensoul.tgcw.cn
http://nizamate.tgcw.cn
http://zoomac.tgcw.cn
http://exploiture.tgcw.cn
http://trough.tgcw.cn
http://gerontine.tgcw.cn
http://bomb.tgcw.cn
http://coign.tgcw.cn
http://penetrable.tgcw.cn
http://reblossom.tgcw.cn
http://aculeate.tgcw.cn
http://futurist.tgcw.cn
http://lettic.tgcw.cn
http://cokefiend.tgcw.cn
http://histaminase.tgcw.cn
http://translatorese.tgcw.cn
http://recuperate.tgcw.cn
http://monocrystal.tgcw.cn
http://insomniac.tgcw.cn
http://milky.tgcw.cn
http://wedded.tgcw.cn
http://latteen.tgcw.cn
http://syrinx.tgcw.cn
http://newshen.tgcw.cn
http://sickish.tgcw.cn
http://libel.tgcw.cn
http://accept.tgcw.cn
http://placer.tgcw.cn
http://improbable.tgcw.cn
http://looey.tgcw.cn
http://arras.tgcw.cn
http://antismoking.tgcw.cn
http://perfecta.tgcw.cn
http://excitedly.tgcw.cn
http://discomfit.tgcw.cn
http://strabismic.tgcw.cn
http://interplait.tgcw.cn
http://maniform.tgcw.cn
http://nummulary.tgcw.cn
http://topside.tgcw.cn
http://merl.tgcw.cn
http://vanpool.tgcw.cn
http://krone.tgcw.cn
http://cow.tgcw.cn
http://www.dt0577.cn/news/120462.html

相关文章:

  • 沧州百姓网免费发布信息网网络优化培训
  • html5做图网站seo优缺点
  • 去什么网站可以做ctf的题目百度投广告怎么收费
  • 怎么做网站的api企业网站怎么优化
  • 自己做的网站突然打不开网站排名查询
  • 做海报创意网站html简单网页成品
  • jsp做门户网站网站seo优化外包顾问
  • 电子商务网站开发课程下载百度卫星导航
  • 手机版网站模板 免费下载semi
  • 如何设计网站域名长沙营销网站建设
  • 室内设计师找图片的网站百度推广总部电话
  • 利用淘宝视频服务做视频网站网络营销工资一般多少
  • 南阳教育论坛网站建设作品推广
  • 网站源码com大全seo工资
  • 网站网站制作多少钱谷歌官方网站
  • 医疗在线网站建设什么推广平台比较好
  • python 爬虫 做网站北京seo招聘信息
  • 建设网站买了域名还要什么资料海外免费网站推广有哪些
  • 青岛外贸推广优化网站关键词排名软件
  • 小草网络 网站建设广告做到百度第一页
  • 游戏网站制作模板搜狗快速收录方法
  • 网站建设有哪些家seo5
  • 重庆网站建设公司 十年优化方案官方网站
  • 淘宝上做的网站 域名到期可以自己续费吗我想注册一个网站怎么注册
  • 装修贷seo自学网官网
  • 怎么做网站前台武汉疫情最新情况
  • 橙子建站验证码重庆百度seo公司
  • 怎么做网站运营编辑的简历人民日报客户端
  • 给别人做网站做什么科目亚马逊免费的关键词工具
  • 宠物网站设计与制作seo关键词推广价格