zoukankan      html  css  js  c++  java
  • springboot

    1、总览

    2、代码

    1)、pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    
    </dependencies>
    View Code

    2)、application.properties

    server.error.whitelabel.enabled=false
    server.error.include-stacktrace=always

    3)、MyController.java

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class MyController {
        @RequestMapping("/")
        public String handler (Model model) {
            throw new RuntimeException("test exception");
        }
    }

    4)、MyCustomErrorController.java

    import org.springframework.boot.web.servlet.error.ErrorController;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * @author www.gomepay.com
     * @date 2019/11/18
     */
    @Controller
    public class MyCustomErrorController implements ErrorController {
    
        @RequestMapping("/error")
        @ResponseBody
        public String handleError(HttpServletRequest request) {
            Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
            Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
            return String.format("<html><body><h2>Error Page</h2><div>Status code: <b>%s</b></div>"
                            + "<div>Exception Message: <b>%s</b></div><body></html>",
                    statusCode, exception==null? "N/A": exception.getMessage());
        }
    
        @Override
        public String getErrorPath() {
            return "/error";
        }
    }

    3、执行

     

  • 相关阅读:
    396 Rotate Function 旋转函数
    395 Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子串
    394 Decode String 字符串解码
    393 UTF-8 Validation UTF-8 编码验证
    392 Is Subsequence 判断子序列
    391 Perfect Rectangle 完美矩形
    390 Elimination Game 淘汰游戏
    389 Find the Difference 找不同
    388 Longest Absolute File Path 最长的绝对文件路径
    387 First Unique Character in a String 字符串中的第一个唯一字符
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/11880375.html
Copyright © 2011-2022 走看看