feat(web): 添加自定义错误控制器处理HTTP错误

- 实现了ErrorController接口来处理错误请求
- 添加了/error端点的错误处理逻辑
- 返回统一格式的错误响应对象R
- 根据请求状态码动态设置错误状态
- 提供了默认500错误码的异常处理机制
This commit is contained in:
zkh
2026-02-14 12:37:02 +08:00
parent 7b346802e0
commit c0448ff6ab
7 changed files with 29 additions and 6 deletions

View File

@ -6,7 +6,7 @@
<groupId>vip.jcfd</groupId>
<artifactId>zkh-framework</artifactId>
<version>1.5.9</version>
<version>1.5.10</version>
<packaging>pom</packaging>
<name>ZKH Framework</name>
<description>A Java framework for ZKH applications</description>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>vip.jcfd</groupId>
<artifactId>zkh-framework</artifactId>
<version>1.5.9</version>
<version>1.5.10</version>
</parent>
<artifactId>zkh-common</artifactId>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>vip.jcfd</groupId>
<artifactId>zkh-framework</artifactId>
<version>1.5.9</version>
<version>1.5.10</version>
</parent>
<artifactId>zkh-data</artifactId>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>vip.jcfd</groupId>
<artifactId>zkh-framework</artifactId>
<version>1.5.9</version>
<version>1.5.10</version>
</parent>
<artifactId>zkh-file</artifactId>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>vip.jcfd</groupId>
<artifactId>zkh-framework</artifactId>
<version>1.5.9</version>
<version>1.5.10</version>
</parent>
<artifactId>zkh-log</artifactId>

View File

@ -7,7 +7,7 @@
<parent>
<groupId>vip.jcfd</groupId>
<artifactId>zkh-framework</artifactId>
<version>1.5.9</version>
<version>1.5.10</version>
</parent>
<artifactId>zkh-web</artifactId>

View File

@ -0,0 +1,23 @@
package vip.jcfd.web.controller;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.jcfd.common.core.R;
import java.util.Optional;
@RestController
public class CustomErrorController implements ErrorController {
@RequestMapping("/error")
public R<?> handleError(HttpServletRequest request) {
int status = Optional.ofNullable(request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE))
.map(Object::toString)
.map(Integer::parseInt)
.orElse(500);
return new R<>(status, "发生错误", false, null);
}
}