ข้อผิดพลาดเริ่มต้นของ Springboot คือการข้ามไปยังหน้าข้อผิดพลาด/ข้อผิดพลาดในเส้นทางการเรนเดอร์ส่งคืนคำขอ
การวิเคราะห์ซอร์สโค้ด: defaulterrorViewResolver.java
Private ModelandView redent (String ViewName, Map <String, Object> Model) {String ErrorViewName = "ข้อผิดพลาด/" + viewName; TemplateavailabilityProvider Provider = this.TemplateavailabilityProviders .getProvider (ErrorViewName, this.applicationContext); if (ผู้ให้บริการ! = null) {ส่งคืน modelandView ใหม่ (ErrorViewName, Model); } return ResolverEsource (ErrorViewName, Model); -ตัวอย่างเช่นใน Application.properites กำหนดค่าหน้าการแสดงผลเป็น
#configure freemakerspring.freeMarker.Template-Loader-Path =/web-inf/
หาก spring.freemarker.template-loader-path,springboot จะค้นหาหน้าการแสดงผลที่ไม่ถูกต้องภายใต้ไฟล์ข้อผิดพลาดในเทมเพลตใน src/main/resources
จากนั้นเมื่อเกิดข้อผิดพลาดระบบจะข้ามไปยังหน้า/web-inf/ข้อผิดพลาด/ข้อผิดพลาด
ใช้ AOP สำหรับการจัดการข้อยกเว้นเว็บเลเยอร์
แพ็คเกจ com.niugang.aop; นำเข้า java.io.ioexception; นำเข้า Javax.servlet.servletexception; นำเข้า Javax.servlet.http.httpservletrequest นำเข้า org.aspectj.lang.annotation. org.slf4j.loggerfactory; นำเข้า org.springframework.stereotype.component; นำเข้า org.springframework.web.context.request.requestattributes; นำเข้า org.springframework.web.context.request.request.request.request.request.request.request.request.request.request. org.springframework.web.context.request.servletrequestattributes; นำเข้า org.springframework.web.servlet.modelandview;/** * ตัวควบคุมการจัดการแบบ Unified Exception LoggerFactory.getLogger (ExceptionControllerascept.class); / ** * วิธีการตัดจุดที่ไม่ระบุชื่อ * * @param ex * @throws servletexception * @throws ioexception */ @afterthrowing (value = "การดำเนินการ (สาธารณะ * com.niugang.controller .. *. * (.. )" ModelAndView (); requestattributes requestattributes = requestcontextholder.getRequestatTributes (); servletrequestattributes r = (servletrequestattributes) requestattributes; คำขอ httpservletrequest = r.getRequest (); ModelAndView.SetViewName ("500"); // ก่อนถ้ามันเป็น runtimeException ถ้า (ตัวอย่างอินสแตนซ์ของ runtimeException) {logger.error ("โยนข้อยกเว้นรันไทม์ {}", ex.getMessage ()); ModelAndView.AdDoBject ("Exception", Ex.getMessage ()); // ข้ามไปที่หน้าข้อผิดพลาด modelandview.addobject ("url", request.getRequesturl ()); Return ModelandView; } modelandview.addobject ("ข้อยกเว้น", "ข้อยกเว้นที่ไม่รู้จัก"); Return ModelandView; -สรุป
ข้างต้นคือสิ่งที่ตัวแก้ไขแนะนำให้คุณทราบเกี่ยวกับการใช้ AOP เพื่อจัดการข้อยกเว้นของ Web Layer ใน Spring Boot ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!