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)、MyController.java

    package com.ebc.controller;
    
    import com.ebc.error.NotYetImplemented;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class MyController {
        /**
         * response status code=500,导航到5xx.html
         */
        @RequestMapping("/")
        public void handleRequest() {
            throw new RuntimeException("test exception");
        }
        /**
         * response status code=501,导航到5xx.html
         */
        @RequestMapping("/app")
        public void handleAppInfoRequest() throws NotYetImplemented {
            throw new NotYetImplemented("The request page is not yet implemented");
        }
    
    }

    3)、ForbiddenException.java

    package com.ebc.error;
    
    import org.springframework.http.HttpStatus;
    import org.springframework.web.bind.annotation.ResponseStatus;
    
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public class ForbiddenException extends Exception {
        public ForbiddenException(String message) {
            super(message);
        }
    }
    View Code

    4)、NotYetImplemented.java

    package com.ebc.error;
    
    import org.springframework.http.HttpStatus;
    import org.springframework.web.bind.annotation.ResponseStatus;
    
    @ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
    public class NotYetImplemented extends Exception {
        public NotYetImplemented(String message) {
            super(message);
        }
    }
    View Code

    5)、5xx.html

    <html>
    <body>
    <h1>My 5xx Error Page</h1>
    <div id="locationDiv"></div>
    <script>
        document.getElementById("locationDiv").innerHTML = "Location: " + window.location.href;
    </script>
    </body>
    </html>

    6)、404.html

    <html>
    <body>
    <h1>My 404 Error Page</h1>
    <div id="locationDiv"></div>
    <script>
        document.getElementById("locationDiv").innerHTML = "Location: " + window.location.href;
    </script></body>
    </html>

    3、执行

    总结:

    1、如果出现了非5xx、404返回码如403时,怎么办?使用error.jsp,并将其放在根下,如本例需放在static目录下。

    2、5xx.html、404.html需放在error目录下。

  • 相关阅读:
    基于微信的域名交易系统(进度汇报之后台)
    基于微信的域名交易系统(数据库表-修订版)
    基于微信的域名交易系统(需求文档)
    基于微信的域名交易系统(功能细节敲定)
    基于微信的域名交易系统(数据库结构设计)
    文档
    你离毕业有多远 原型设计pro
    Current Position of GradPaul 毕业宝四月中旬进度
    Architecture of GradPaul 毕业宝架构设计
    翘课老黄历——设计文档
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/11873409.html
Copyright © 2011-2022 走看看