Bläddra i källkod

增加404页面

xiehui 3 år sedan
förälder
incheckning
818c8f93d0

+ 28 - 0
spring-wechars/src/main/java/cn/org/spring/wechar/config/ErrorPageConfig.java

@@ -0,0 +1,28 @@
+package cn.org.spring.wechar.config;
+
+import org.springframework.boot.web.server.ErrorPage;
+import org.springframework.boot.web.server.ErrorPageRegistrar;
+import org.springframework.boot.web.server.ErrorPageRegistry;
+import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author: 谢辉
+ * @date: 2021/5/20
+ * @email: xieh_mail@163.com
+ * @description: 错误页面配置
+ * @modifiedBy:
+ * @version: 1.0
+ */
+@Component
+public class ErrorPageConfig implements ErrorPageRegistrar {
+    @Override
+    public void registerErrorPages(ErrorPageRegistry registry) {
+        //1、按错误的类型显示错误的网页
+        //错误类型为404,找不到网页的,默认显示404.html网页
+        ErrorPage e404 = new ErrorPage(HttpStatus.NOT_FOUND, "/common/error/404");
+        //错误类型为500,表示服务器响应错误,默认显示500.html网页
+        ErrorPage e500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/common/error/500");
+        registry.addErrorPages(e404, e500);
+    }
+}

+ 3 - 1
spring-wechars/src/main/java/cn/org/spring/wechar/config/WebConfig.java

@@ -37,7 +37,9 @@ public class WebConfig implements WebMvcConfigurer {
                 "/druid/*",
                 "/favicon.ico",
                 "/**/view/**",
-                "/error");
+                "/error",
+                "/**/isWorker",
+                "/common/**");
     }
 
 

+ 27 - 0
spring-wechars/src/main/java/cn/org/spring/wechar/controller/CommonController.java

@@ -0,0 +1,27 @@
+package cn.org.spring.wechar.controller;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+/**
+ * @author: 谢辉
+ * @date: 2021/5/20
+ * @email: xieh_mail@163.com
+ * @description:
+ * @modifiedBy:
+ * @version: 1.0
+ */
+@Controller
+@RequestMapping("/common")
+public class CommonController {
+
+    @RequestMapping("/error/404")
+    public String error404() {
+        return "error/404";
+    }
+
+    @RequestMapping("/error/500")
+    public String error500() {
+        return "error/500";
+    }
+}

+ 1 - 0
spring-wechars/src/main/java/cn/org/spring/wechar/controller/WeCharController.java

@@ -51,6 +51,7 @@ public class WeCharController {
      */
     @GetMapping("/isWorker")
     public String isWorker() {
+        //int i = 10 / 0;
         return "Is Worker !";
     }
 

+ 16 - 4
spring-wechars/src/main/java/cn/org/spring/wechar/exception/ExceptionControllerAdvice.java

@@ -2,8 +2,11 @@ package cn.org.spring.wechar.exception;
 
 import cn.org.spring.wechar.bean.Result;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
 
 /**
  * @author: 谢辉
@@ -14,12 +17,21 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
  * @version: 1.0
  */
 @Slf4j
-@RestControllerAdvice(basePackages = "cn.org.spring.wechar.**.controller")
+@ControllerAdvice(basePackages = "cn.org.spring.wechar")
 public class ExceptionControllerAdvice {
 
-    @ExceptionHandler(value = Throwable.class)
-    public Result handleException(Throwable throwable) {
+    /**
+     * 响应json数据
+     *
+     * @param throwable
+     * @return
+     */
+    @ResponseBody
+    @ExceptionHandler(value = Exception.class)
+    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
+    public Result handleException(Exception throwable) {
         log.error("错误异常{}", throwable);
         return Result.fail("发生未知错误,稍后在试~");
     }
+
 }

+ 2 - 1
spring-wechars/src/main/resources/application.yml

@@ -23,7 +23,8 @@ spring:
   # redis 配置
   redis:
     # 地址
-    host: 192.168.2.55
+    #host: 192.168.2.55
+    host: 127.0.0.1
     # 端口,默认为6379
     port: 6379
     # 数据库索引

+ 10 - 0
spring-wechars/src/main/resources/templates/error/404.html

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Title</title>
+</head>
+<body>
+<h1>404</h1>
+</body>
+</html>

+ 10 - 0
spring-wechars/src/main/resources/templates/error/500.html

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Title</title>
+</head>
+<body>
+<h1>500</h1>
+</body>
+</html>