使用 @controlleradvice 结合 @exceptionhandler 可以捕获主题不存在异常。具体做法是定义一个全局异常处理类,在方法上指定要捕获的异常类型(如 nohandlerfoundexception 或自定义的主题缺失异常),并返回统一的错误响应结构。

如何配置全局异常处理器以拦截未知路径请求?
通过实现 ErrorController 接口或注册 SimpleMappingExceptionResolver 可以处理 Spring Boot 默认的错误页面逻辑。但在现代 Spring Boot (2.x/3.x) 中,推荐直接使用 @RestControllerAdvice 来接管所有未处理的异常。对于“主题不存在”这种业务语义,通常对应的是资源未找到(404)或特定的业务异常。如果是指 Web 请求路径对应的 Controller 方法不存在,Spring Boot 默认会抛出 NoHandlerFoundException。我们需要在配置中启用该异常抛出,并在全局处理器中捕获它。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@RestControllerAdvice
class GlobalExceptionHandler {
@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<string> handleNotFound(NoHandlerFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("<h1>404 Not Found</h1>
<p>The requested resource is not available.</p>");
}
@ExceptionHandler(Exception.class)
public ResponseEntity<string> handleGeneral(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("<h1>500 Internal Server Error</h1>
<p>An unexpected error occurred.");
}
}</p></string></string>
运行上述代码后,访问一个不存在的 URL(如 /nonexistent),将返回 HTML 格式的错误信息。注意:Spring Boot 默认会将 404 转发到内置的错误页面,而不是抛出 NoHandlerFoundException。因此,必须在 application.properties 中设置 spring.mvc.throw-exception-if-no-handler-found=true 和 spring.web.resources.add-mappings=false 才能触发此异常捕获流程。
如何自定义主题缺失的业务异常及其处理逻辑?
如果“主题不存在”指的是业务层面的数据缺失(例如邮件主题、文章主题等),应创建自定义异常类 TopicNotFoundException。在全局异常处理器中单独捕获该异常,以便返回更精确的业务错误码和提示信息。这种方式比捕获通用异常更安全,且能区分系统错误与业务逻辑错误。

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
public class TopicNotFoundException extends RuntimeException {
public TopicNotFoundException(String message) {
super(message);
}
}
@RestControllerAdvice
class BusinessExceptionHandler {
@ExceptionHandler(TopicNotFoundException.class)
public ResponseEntity<string> handleTopicNotFound(TopicNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("<h1>404 Topic Not Found</h1>
<p>Error: " + ex.getMessage() + "</p>");
}
}</string>
当业务代码中主动抛出 new TopicNotFoundException("Subject 'News' does not exist") 时,全局处理器将捕获并返回包含具体错误信息的 HTML 响应。这确保了前端或调用方能清晰识别错误原因。
当主题不存在异常被捕获后,返回的 HTTP 状态码是什么?
在 @RestControllerAdvice 中,若未显式指定状态码,Spring 默认会根据异常类型映射到相应 HTTP 状态。对于 NoHandlerFoundException,通常映射为 404;对于自定义的 TopicNotFoundException,若未设置状态码,可能默认为 500 或根据父类 Exception 的行为而定。因此,最佳实践是在 ResponseEntity 中明确指定状态码,如 .status(HttpStatus.NOT_FOUND),以确保行为一致且符合 RESTful 规范。











