In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the example analysis of the SpringBoot global exception error page, the content is very detailed, interested friends can refer to, hope to be helpful to you.
SpringBoot global exception error page 1. To create a project, rely on Web and thymeleaforg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-thymeleaf2. Prepare two exception error pages
Create an error folder in template, 4xx.html and 5xx.html in an error folder
3. Prepare a custom error handling class
Com.zhl.springbootexceptionjunit.controller.MyBasicErrorController
SpringBoot will look for / error view when there is an exception. If not, it will look for error static pages such as error/400.html according to the error code.
Rewrite the ErrorController here to implement the / error view and jump to the corresponding view according to the error type. Due to the use of view technology, you need to refer to the template engine (thymeleaf in this example)
Package com.zhl.springbootexceptionjunit.controller;import org.springframework.boot.web.servlet.error.ErrorController;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.context.request.ServletWebRequest;import org.springframework.web.context.request.WebRequest;import javax.servlet.http.HttpServletRequest;import java.util.Date;import java.util.HashMap;import java.util.Map / * customized ErrorController to enable SpringBoot to find its own custom error page * most of the code BasicController is consistent, the key point is to modify the path of the error page * / @ Controller@RequestMapping (value = "/ error") public class MyBasicErrorController implements ErrorController {@ RequestMapping (produces = {"text/html"}) / / return it to the browser public String handlerError (HttpServletRequest request, Model model) {WebRequest webRequest = new ServletWebRequest (request) / / A pair of request is packaged in order to operate more methods HttpStatus status = this.getStatus (request); / / get status String path = (String) webRequest.getAttribute ("javax.servlet.error.request_uri", 0); String message = (String) webRequest.getAttribute ("javax.servlet.error.message", 0); if (message.equals (")) {message =" No Available Message " } / / carrying error data information model.addAttribute ("timestamp", new Date ()); model.addAttribute ("statusCode", status.value ()); model.addAttribute ("error", status.getReasonPhrase ()); model.addAttribute ("message", message); model.addAttribute ("path", path); int I = status.value () / 100 / determine whether the 4xx or 5xx error if (I = = 4) {return "error/4xx"; / / use the custom error page} else if (I = = 5) {return "error/5xx"; / / use the custom error page} return null;} @ RequestMapping// to return to the client public ResponseEntity error (HttpServletRequest request) {WebRequest webRequest = new ServletWebRequest (request) / / A pair of request is packaged in order to operate more methods HttpStatus status = this.getStatus (request); / / get status Map map = new HashMap (); if (status = = HttpStatus.NO_CONTENT) {return new ResponseEntity (status);} else {String path = (String) webRequest.getAttribute ("javax.servlet.error.request_uri", 0) String message = (String) webRequest.getAttribute ("javax.servlet.error.message", 0); map.put ("timestamp", new Date ()); map.put ("statusCode", status.value ()); map.put ("error", status.getReasonPhrase ()); map.put ("message", message); map.put ("path", path) Return new ResponseEntity (map, status);} protected HttpStatus getStatus (HttpServletRequest request) {Integer statusCode = (Integer) request.getAttribute ("javax.servlet.error.status_code"); if (statusCode = = null) {return HttpStatus.INTERNAL_SERVER_ERROR;} else {try {return HttpStatus.valueOf (statusCode);} catch (Exception var4) {return HttpStatus.INTERNAL_SERVER_ERROR @ Override public String getErrorPath () {return "null";}} 4. Test code
Com.zhl.springbootexceptionjunit.controller.UsersController
@ Controllerpublic class UsersController {@ RequestMapping ("showinfo") public String showinfo () {String str=null; str.length (); return "ok";}}
Test 1, runtime error:
Test 2: no resources
This is the end of the example analysis of the SpringBoot global exception error page. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.