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

青岛做企业网站公司网站制作

青岛做企业网站,公司网站制作,剪映导出的视频字幕有乱码,怎么购买网站空间和域名SpringBoot错误处理----源码解析 文章目录 1、默认机制2、使用ExceptionHandler标识一个方法,处理用Controller标注的该类发生的指定错误1).局部错误处理部分源码2).测试 3、 创建一个全局错误处理类集中处理错误,使用Controller…

SpringBoot错误处理----源码解析

文章目录

    • 1、默认机制
    • 2、使用@ExceptionHandler标识一个方法,处理用@Controller标注的该类发生的指定错误
      • 1).局部错误处理部分源码
      • 2).测试
    • 3、 创建一个全局错误处理类集中处理错误,使用==@ControllerAdvice==注解标注
      • 1).全局错误处理部分源码
      • 3).测试
    • 4、SpringMVC错误处理未能处理(上述处理不存在)
        • 1、SpringBoot中自动配置的错误处理机制,基于`ErrorMvcAutoConfiguration`自动配置实现
        • 2、` BasicErrorController`组件
        • 规则如下:
    • 5、自定义错误响应
    • 6、测试
        • 1、在项目的resources/templates/目录下创建一个error文件夹,放入4xx.html、5xx.html
        • 2、在项目的resources/templates/error文件夹中,再放入404.html、500.html
        • 3、浏览器先后测试上述情况

1、默认机制

SpringBoot错误处理的自动配置都在ErrorMvcAutoConfiguration中,两大核心机制:

  • 1.SpringBoot会自适应处理错误,响应页面或JSON数据

  • 2.SpringMVC的错误处理机制依然保留,MVC处理不了,才会交给SpringBoot进行处理。
    在这里插入图片描述(图片来自尚硅谷)

2、使用@ExceptionHandler标识一个方法,处理用@Controller标注的该类发生的指定错误

(默认只能处理这个类的该指定错误)

// @ExceptionHandler源码
@Target({ElementType.METHOD})     // 指定了该注解仅能用于方法
@Retention(RetentionPolicy.RUNTIME)   //指定了注解会在运行时保留,这允许通过反射在运行时获取对注解的访问
@Documented      //注解应该被包含在生成的 JavaDoc 文档中
@Reflective({ExceptionHandlerReflectiveProcessor.class}) 
public @interface ExceptionHandler {Class<? extends Throwable>[] value() default {};       //它定义了一个名为 value 的属性,其类型是一个 Class 数组,这个数组的元素必须是 Throwable 类或其子类。通过使用这个注解时,可以为 value 属性提供一个 Throwable 类型的数组
}

1).局部错误处理部分源码

@Controller         //适配服务端渲染    前后不分离模式开始
public class WelcomeController {//来到首页@GetMapping("/")public String index(){int i=10/0;    //制造错误return "index";}@ResponseBody     //返回的字符串应该直接作为 HTTP 响应体的内容,而不是作为视图名称解析。通常用于返回 JSON 或纯文本等非HTML内容@ExceptionHandler(Exception.class)   //传递到value数组中public String handleException(Exception e){return "Ohho~~~,原因:"+e.getMessage();}}

2).测试

  • 1.启动项目,浏览器访问http://localhost:8080/

  • 2.测试结果(成功处理错误)
    在这里插入图片描述

3、 创建一个全局错误处理类集中处理错误,使用==@ControllerAdvice==注解标注

(这个类是集中处理所有@Controller 发生的错误)

//@ControllerAdvice源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component       //标记类为一个 Spring 组件
public @interface ControllerAdvice {@AliasFor(annotation = Component.class,attribute = "value")String name() default "";@AliasFor("basePackages")String[] value() default {};@AliasFor("value")String[] basePackages() default {};Class<?>[] basePackageClasses() default {};Class<?>[] assignableTypes() default {};Class<? extends Annotation>[] annotations() default {};
}

1).全局错误处理部分源码

@ControllerAdvice        //这个类是集中处理所有@Controller发生的错误
public class GlobalExceptionHandler {@ResponseBody@ExceptionHandler(Exception.class)public String handleException(Exception e){return "Ohho~~~统一处理所有错误,原因:"+e.getMessage();}
}

###2).再创建一个类,使用@Controller注解标注

@Controller
public class HelloController {@GetMapping("/haha")public String haha(){int i = 10/0;        //制造错误return "index";}
}

3).测试

  • 1.启动项目,浏览器访问http://localhost:8080/
  • 2.测试结果(成功处理错误)就近原则,执行的是@Controller类中标注了@ExceptionHandler方法的处理
    在这里插入图片描述
  • 3.浏览器访问http://localhost:8080/haha全局错误处理类进行处理
    在这里插入图片描述

4、SpringMVC错误处理未能处理(上述处理不存在)

第一阶段的处理未解决,错误转发到/error,执行后续处理(图中第一阶段失效,第二阶段处理),以下测试过程中,注释掉上文中的全局和局部处理代码

1、SpringBoot中自动配置的错误处理机制,基于ErrorMvcAutoConfiguration自动配置实现

ErrorMvcAutoConfiguration自动装配类中,SpringBoot在底层写好一个 ==BasicErrorController==的组件,专门处理/error这个请求,部分源码如下:

@AutoConfiguration(before = WebMvcAutoConfiguration.class) //指定了该自动配置类在 WebMvcAutoConfiguration 之前进行配置
@ConditionalOnWebApplication(type = Type.SERVLET)    //只有在当前应用是一个 Servlet Web 应用时,这个自动配置才会生效
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class })   //只有当类路径中存在 Servlet 和 DispatcherServlet 时,这个自动配置才会生效
@EnableConfigurationProperties({ ServerProperties.class, WebMvcProperties.class }) //启用指定类的配置属性绑定
public class ErrorMvcAutoConfiguration {private final ServerProperties serverProperties; //注入了 ServerProperties 实例。这样的构造方法注入是为了获取应用程序的服务器配置public ErrorMvcAutoConfiguration(ServerProperties serverProperties) {this.serverProperties = serverProperties;  //注入 ServerProperties 实例}//容器中不存在 ErrorAttributes 类型的 Bean 时,才会创建并注册当前方法所返回的 Bean@Bean@ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)public DefaultErrorAttributes errorAttributes() {return new DefaultErrorAttributes();}//在容器中不存在 ErrorController 类型的 Bean 时,才会创建并注册当前方法所返回的 Bean@Bean@ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)public BasicErrorController basicErrorController(ErrorAttributes errorAttributes,ObjectProvider<ErrorViewResolver> errorViewResolvers) {return new BasicErrorController(errorAttributes, this.serverProperties.getError(),errorViewResolvers.orderedStream().toList());}...
}
2、 BasicErrorController组件

SpringBoot中默认的server.error.path=/error,即该类就是处理/error请求的,根据不同类型的请求,如果产生 HTML 内容的请求,匹配public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) 这个方法;否则其他请求类型,匹配public ResponseEntity<Map<String, Object>> error(HttpServletRequest request)方法。分别进行处理。(只会匹配其中一个,Spring MVC会尝试匹配与请求路径最匹配的RequestMapping)

  • 1.部分源码:
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {private final ErrorProperties errorProperties;/*** Create a new {@link BasicErrorController} instance.* @param errorAttributes the error attributes* @param errorProperties configuration properties*/public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {this(errorAttributes, errorProperties, Collections.emptyList());}/*** Create a new {@link BasicErrorController} instance.* @param errorAttributes the error attributes* @param errorProperties configuration properties* @param errorViewResolvers error view resolvers*/public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties,List<ErrorViewResolver> errorViewResolvers) {super(errorAttributes, errorViewResolvers);Assert.notNull(errorProperties, "ErrorProperties must not be null");this.errorProperties = errorProperties;}@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)   //请求类型为HTML 文本public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {HttpStatus status = getStatus(request);Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML)));response.setStatus(status.value());ModelAndView modelAndView = resolveErrorView(request, response, status, model);return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);}@RequestMappingpublic ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {HttpStatus status = getStatus(request);if (status == HttpStatus.NO_CONTENT) {return new ResponseEntity<>(status);}Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));return new ResponseEntity<>(body, status);}...
}
  • 2.匹配成功之后,错误页面解析的核心代码

    //1、解析错误的自定义视图地址
    ModelAndView modelAndView = resolveErrorView(request, response, status, model);
    //2、如果解析不到错误页面的地址,默认的错误页就是 error
    return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);
    
  • 3.在ErrorMvcAutoConfiguration自动装配类中,SpringBoot在底层写好一个 ==DefaultErrorViewResolver==的组件,注入到了容器中

    	@Configuration(proxyBeanMethods = false)@EnableConfigurationProperties({ WebProperties.class, WebMvcProperties.class })static class DefaultErrorViewResolverConfiguration {private final ApplicationContext applicationContext;private final Resources resources;DefaultErrorViewResolverConfiguration(ApplicationContext applicationContext, WebProperties webProperties) {this.applicationContext = applicationContext;this.resources = webProperties.getResources();}@Bean@ConditionalOnBean(DispatcherServlet.class)@ConditionalOnMissingBean(ErrorViewResolver.class)DefaultErrorViewResolver conventionErrorViewResolver() {return new DefaultErrorViewResolver(this.applicationContext, this.resources);}}
    
  • 4.在DefaultErrorViewResolver中定义了错误页的默认规则,功能如下:

    • 如果在类路径下,查看是否存在 error/错误码 的页面,存在就返回该视图;
    • 否则,去4个CLASSPATH_RESOURCE_LOCATIONS路径下,查看是否写了 error/错误码.html 的页面,存在则返回该视图;
    • 都不存在,则modelAndView=null;则判断是否有 error/4xx 或者 error/5xx 的页面,存在则返回该视图
    • 否则,去4个CLASSPATH_RESOURCE_LOCATIONS路径下,查看是否写了 error/4xx.html 或者 error/5xx.html 的页面,存在则返回该视图;
    public class DefaultErrorViewResolver implements ErrorViewResolver, Ordered {...private static final Map<Series, String> SERIES_VIEWS;static {Map<Series, String> views = new EnumMap<>(Series.class);views.put(Series.CLIENT_ERROR, "4xx");views.put(Series.SERVER_ERROR, "5xx");SERIES_VIEWS = Collections.unmodifiableMap(views);}// 查看是否存在 error/错误码 的页面,存在就返回该视图// 不存在,则判断是否有  error/4xx  或者  error/5xx  的页面,存在则返回该视图@Overridepublic ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {ModelAndView modelAndView = resolve(String.valueOf(status.value()), model);if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) {modelAndView = resolve(SERIES_VIEWS.get(status.series()), model);}return modelAndView;}// 在类路径下,查看是否存在 error/错误码 的模板引擎,存在就返回该视图;// 否则去下面的4个CLASSPATH_RESOURCE_LOCATIONS路径下,查看是否写了  error/错误码.html 的页面,存在则返回该视图private ModelAndView resolve(String viewName, Map<String, Object> model) {String errorViewName = "error/" + viewName;TemplateAvailabilityProvider provider = this.templateAvailabilityProviders.getProvider(errorViewName,this.applicationContext);if (provider != null) {return new ModelAndView(errorViewName, model);}return resolveResource(errorViewName, model);}//去下面的4个CLASSPATH_RESOURCE_LOCATIONS路径下,查看是否写了  error/错误码.html  的页面,存在则返回该视图//this.resources.getStaticLocations()//private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",//			"classpath:/resources/", "classpath:/static/", "classpath:/public/" };private ModelAndView resolveResource(String viewName, Map<String, Object> model) {for (String location : this.resources.getStaticLocations()) {try {Resource resource = this.applicationContext.getResource(location);resource = resource.createRelative(viewName + ".html");if (resource.exists()) {return new ModelAndView(new HtmlResourceView(resource), model);}}catch (Exception ex) {}}return null;}...
    }
    
  • 5.在ErrorMvcAutoConfiguration自动装配类中,向容器中放入了一个默认名为error的视图,提供了默认的白页功能,如果上述都无法处理

    	@Configuration(proxyBeanMethods = false)@ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", matchIfMissing = true)@Conditional(ErrorTemplateMissingCondition.class)protected static class WhitelabelErrorViewConfiguration {private final StaticView defaultErrorView = new StaticView();//   注入了error视图@Bean(name = "error")@ConditionalOnMissingBean(name = "error")public View defaultErrorView() {return this.defaultErrorView;}// If the user adds @EnableWebMvc then the bean name view resolver from// WebMvcAutoConfiguration disappears, so add it back in to avoid disappointment.@Bean@ConditionalOnMissingBeanpublic BeanNameViewResolver beanNameViewResolver() {BeanNameViewResolver resolver = new BeanNameViewResolver();resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);return resolver;}}
    

    默认的白页源码,就在类ErrorMvcAutoConfiguration中定义的

    private static class StaticView implements View {private static final MediaType TEXT_HTML_UTF8 = new MediaType("text", "html", StandardCharsets.UTF_8);private static final Log logger = LogFactory.getLog(StaticView.class);@Overridepublic void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)throws Exception {if (response.isCommitted()) {String message = getMessage(model);logger.error(message);return;}response.setContentType(TEXT_HTML_UTF8.toString());StringBuilder builder = new StringBuilder();Object timestamp = model.get("timestamp");Object message = model.get("message");Object trace = model.get("trace");if (response.getContentType() == null) {response.setContentType(getContentType());}builder.append("<html><body><h1>Whitelabel Error Page</h1>").append("<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>").append("<div id='created'>").append(timestamp).append("</div>").append("<div>There was an unexpected error (type=").append(htmlEscape(model.get("error"))).append(", status=").append(htmlEscape(model.get("status"))).append(").</div>");if (message != null) {builder.append("<div>").append(htmlEscape(message)).append("</div>");}if (trace != null) {builder.append("<div style='white-space:pre-wrap;'>").append(htmlEscape(trace)).append("</div>");}builder.append("</body></html>");response.getWriter().append(builder.toString());}private String htmlEscape(Object input) {return (input != null) ? HtmlUtils.htmlEscape(input.toString()) : null;}private String getMessage(Map<String, ?> model) {Object path = model.get("path");String message = "Cannot render error page for request [" + path + "]";if (model.get("message") != null) {message += " and exception [" + model.get("message") + "]";}message += " as the response has already been committed.";message += " As a result, the response may have the wrong status code.";return message;}@Overridepublic String getContentType() {return "text/html";}}
    
  • 6.在ErrorMvcAutoConfiguration自动装配类中,封装了JSON格式的错误信息(初始化了错误的类型、错误的状态码、路径、栈信息、时间戳等信息)

    @Bean
    @ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)
    public DefaultErrorAttributes errorAttributes() {return new DefaultErrorAttributes();
    }
    
规则如下:
  • 1.解析一个错误页:

    • 如果发生了500、404、503、403 这些错误
        1. 如果有模板引擎,默认在 classpath:/templates/error/精确码.html
        1. 如果没有模板引擎,在静态资源文件夹下找 精确码.html
    • 如果匹配不到精确码.html这些精确的错误页,就去找5xx.html4xx.html模糊匹配
        1. 如果有模板引擎,默认在 classpath:/templates/error/5xx.html
        1. 如果没有模板引擎,在静态资源文件夹下找 5xx.html
  • 2.如果模板引擎路径templates下有 error.html页面,就直接渲染

5、自定义错误响应

​ 1) 自定义json响应:使用文章第2和第3部分介绍的,使用注解进行统一的异常处理

​ 2)自定义页面响应:根据第4部分介绍的规则,在对应的项目路径"classpath:/METAINF/resources/","classpath:/resources/","classpath:/static/", "classpath:/public/"或者模板引擎目录下,定义错误页面即可。

6、测试

1、在项目的resources/templates/目录下创建一个error文件夹,放入4xx.html、5xx.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>4xx.html
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
5xx.html
</body>
</html>
2、在项目的resources/templates/error文件夹中,再放入404.html、500.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
404.html
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
500.html
</body>
</html>
3、浏览器先后测试上述情况
  • 1.访问一个不存在的路径;
  • 2.制造除0错误。

文章转载自:
http://quebracho.nLkm.cn
http://zoologer.nLkm.cn
http://jacksonville.nLkm.cn
http://assentation.nLkm.cn
http://rx.nLkm.cn
http://majorca.nLkm.cn
http://wallet.nLkm.cn
http://harleian.nLkm.cn
http://kure.nLkm.cn
http://fuel.nLkm.cn
http://wham.nLkm.cn
http://parodontal.nLkm.cn
http://nibmar.nLkm.cn
http://swordbill.nLkm.cn
http://wordmongering.nLkm.cn
http://triallelic.nLkm.cn
http://monticulate.nLkm.cn
http://ovenwood.nLkm.cn
http://herdman.nLkm.cn
http://trim.nLkm.cn
http://launder.nLkm.cn
http://bushfighting.nLkm.cn
http://munitioner.nLkm.cn
http://oomingmack.nLkm.cn
http://reinterpret.nLkm.cn
http://interdiffuse.nLkm.cn
http://galen.nLkm.cn
http://beak.nLkm.cn
http://squeal.nLkm.cn
http://unhealthiness.nLkm.cn
http://ensate.nLkm.cn
http://anthropomorphosis.nLkm.cn
http://alibility.nLkm.cn
http://vibrator.nLkm.cn
http://agedly.nLkm.cn
http://flee.nLkm.cn
http://vaesite.nLkm.cn
http://remediable.nLkm.cn
http://lobsterback.nLkm.cn
http://ordines.nLkm.cn
http://circumjovial.nLkm.cn
http://core.nLkm.cn
http://aerobomb.nLkm.cn
http://unmixed.nLkm.cn
http://paraphrasis.nLkm.cn
http://ropery.nLkm.cn
http://gallicanism.nLkm.cn
http://desize.nLkm.cn
http://wed.nLkm.cn
http://cleo.nLkm.cn
http://carbonade.nLkm.cn
http://reincrease.nLkm.cn
http://provostship.nLkm.cn
http://patience.nLkm.cn
http://philosophical.nLkm.cn
http://minification.nLkm.cn
http://commandership.nLkm.cn
http://machisma.nLkm.cn
http://unfillable.nLkm.cn
http://elucidation.nLkm.cn
http://errantry.nLkm.cn
http://noseglasses.nLkm.cn
http://amortize.nLkm.cn
http://strategist.nLkm.cn
http://negligible.nLkm.cn
http://wollastonite.nLkm.cn
http://energize.nLkm.cn
http://gliding.nLkm.cn
http://glyphography.nLkm.cn
http://diethyl.nLkm.cn
http://photorealism.nLkm.cn
http://leucocidin.nLkm.cn
http://xenelasia.nLkm.cn
http://iaea.nLkm.cn
http://preordination.nLkm.cn
http://imprimis.nLkm.cn
http://meadowland.nLkm.cn
http://antispasmodic.nLkm.cn
http://rutherford.nLkm.cn
http://tripos.nLkm.cn
http://histosol.nLkm.cn
http://universal.nLkm.cn
http://catalepsis.nLkm.cn
http://bivinyl.nLkm.cn
http://auscultation.nLkm.cn
http://silicidize.nLkm.cn
http://subdominant.nLkm.cn
http://aptitude.nLkm.cn
http://emitter.nLkm.cn
http://kampala.nLkm.cn
http://estelle.nLkm.cn
http://anencephalic.nLkm.cn
http://unchoke.nLkm.cn
http://chelicera.nLkm.cn
http://dermatology.nLkm.cn
http://airways.nLkm.cn
http://flightless.nLkm.cn
http://fugue.nLkm.cn
http://jerrycan.nLkm.cn
http://grit.nLkm.cn
http://www.hrbkazy.com/news/66066.html

相关文章:

  • 贵阳网站建设管理杭州seo
  • wordpress修改管理密码错误seo教程网站优化
  • 阿里云网站部署自己可以做网站吗
  • 网店网站开发郑州网站建设公司排行榜
  • 网站推广连接怎么做的优化
  • 衡水做wap网站多少钱企业网站怎么注册
  • 微信扫码关注登陆wordpress廊坊网站排名优化公司哪家好
  • 淘宝客做网站备注怎么写的百度免费推广方法
  • 网站一般如何做搜索功能典型的口碑营销案例
  • 网站域名管理中心交换链接营销案例
  • php手机网站怎么做怎么建网页
  • 临沂做进销存网站国外产品推广平台
  • 建设公司网站价格如何做谷歌优化
  • 自适应网站用什么软件设计百度推广多少钱一天
  • 网站和微信公众号建设重庆电子商务网站seo
  • 网站建设业务拓展思路网络最有效的推广方法
  • 果农在哪些网站做推广seowhy教研室
  • 网站会员充值接口怎么做的万网官网域名注册
  • 义乌水务建设集团官方网站南京seo排名优化公司
  • 凡客诚品网站设计网络培训心得体会5篇
  • 做网站需要的素材照片哪里可以学网络运营和推广
  • 南京快速建站模板下载爱站网注册人查询
  • 网站建设十胜石深圳seo培训
  • 台前网站建设价格百度怎么搜索网址打开网页
  • 网站备案一般要多久网站seo价格
  • 上海手机网站建设电话能打开各种网站的搜索引擎
  • 新蔡哪有做网站建设的代发百度关键词排名
  • 教育局两学一做网站seo刷关键词排名软件
  • 做美食网站的目的襄阳seo优化排名
  • 长沙建设企业网站综合型b2b电子商务平台网站