异常

@Controller@ControllerAdvice 类可以有 @ExceptionHandler 方法来处理来自控制器方法的异常。以下示例包含这样一个处理方法

  • Java

  • Kotlin

import java.io.IOException;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Controller
public class SimpleController {

	@ExceptionHandler(IOException.class)
	public ResponseEntity<String> handle() {
		return ResponseEntity.internalServerError().body("Could not read file storage");
	}

}
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.ExceptionHandler
import java.io.IOException

@Controller
class SimpleController {

	@ExceptionHandler(IOException::class)
	fun handle() : ResponseEntity<String> {
		return ResponseEntity.internalServerError().body("Could not read file storage")
	}
	
}

异常可以匹配正在传播的顶级异常(即直接抛出的 IOException)或顶级包装异常内的直接原因(例如,包装在 IllegalStateException 中的 IOException)。

对于匹配的异常类型,最好将目标异常声明为方法参数,如前例所示。或者,注解声明可以缩小要匹配的异常类型范围。我们通常建议在参数签名中尽可能具体,并在具有相应顺序优先级的 @ControllerAdvice 上声明主要根异常映射。有关详细信息,请参阅 MVC 部分

WebFlux 中的 @ExceptionHandler 方法支持与 @RequestMapping 方法相同的方法参数和返回值,但请求体和 @ModelAttribute 相关的方法参数除外。

Spring WebFlux 中对 @ExceptionHandler 方法的支持由 @RequestMapping 方法的 HandlerAdapter 提供。有关更多详细信息,请参阅 DispatcherHandler

媒体类型映射

除了异常类型,@ExceptionHandler 方法还可以声明可产生的媒体类型。这允许根据 HTTP 客户端请求的媒体类型(通常在 "Accept" HTTP 请求头中)来精细化错误响应。

应用程序可以直接在注解上声明可产生的媒体类型,对于相同的异常类型

  • Java

  • Kotlin

@ExceptionHandler(produces = "application/json")
public ResponseEntity<ErrorMessage> handleJson(IllegalArgumentException exc) {
	return ResponseEntity.badRequest().body(new ErrorMessage(exc.getMessage(), 42));
}

@ExceptionHandler(produces = "text/html")
public String handle(IllegalArgumentException exc, Model model) {
	model.addAttribute("error", new ErrorMessage(exc.getMessage(), 42));
	return "errorView";
}
@ExceptionHandler(produces = ["application/json"])
fun handleJson(exc: IllegalArgumentException): ResponseEntity<ErrorMessage> {
	return ResponseEntity.badRequest().body(ErrorMessage(exc.message, 42))
}

@ExceptionHandler(produces = ["text/html"])
fun handle(exc: IllegalArgumentException, model: Model): String {
	model.addAttribute("error", ErrorMessage(exc.message, 42))
	return "errorView"
}

在这里,方法处理相同的异常类型,但不会被拒绝为重复。相反,请求 "application/json" 的 API 客户端将接收到 JSON 错误,浏览器将获得 HTML 错误视图。每个 @ExceptionHandler 注解可以声明多个可产生的媒体类型,错误处理阶段的内容协商将决定使用哪种内容类型。

方法参数

@ExceptionHandler 方法支持与 @RequestMapping 方法相同的方法参数,只是请求体可能已经被消费。

返回值

@ExceptionHandler 方法支持与 @RequestMapping 方法相同的返回值