【发布时间】:2019-08-12 20:48:08
【问题描述】:
在Spring Webflux中,首选的异常处理方式是什么?
@RestControllerAdvice 来自 Spring MVC 而 DefaultErrorAttributes 来自 Spring Webflux。
然而,在 Spring Webflux 中有人可以使用 @RestControllerAdvice。有什么优点/缺点?
@RestControllerAdvice
@RestControllerAdvice
public class ControllerAdvice
{
@ExceptionHandler(Throwable.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Mono<Map<String, Object>> exceptions(Throwable e)
{
return Mono.just(Map.of("message", "bad"));
}
}
扩展 DefaultErrorAttributes
@Component
public class ErrorAttributes extends DefaultErrorAttributes
{
@Override
public Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace)
{
var ex = getError(request);
var attributes = new LinkedHashMap<String, Object>();
attributes.put("status", HttpStatus.BAD_REQUEST.value());
attributes.put("message", "bad");
return attributes;
}
}
我想留在响应式世界中,所以我更倾向于 DefaultErrorAttributes(它与 Webflux 中的 DefaultErrorWebExceptionHandler 配合得很好)。但是,在@RestControllerAdvice 中,我也可以使用 Mono.just(...)。
【问题讨论】:
-
我注意到它“已经有一段时间了”并且没有任何回应。这是因为它是“偏好的东西”吗?我对 Webflux 也很陌生,很高兴听到那些做这件事的人的来信(即,我相当确定扩展 DefaultErrorAttributes 是“一个附加选项”是有原因的" 在 Webflux 中)。
标签: spring spring-boot exception error-handling spring-webflux