创建自定义 ErrorWebExceptionHandler 失败

Creating custom ErrorWebExceptionHandler fails

我正在尝试通过扩展默认启动 2 在 Spring 启动 2 中创建我自己的 ErrorWebExceptionHandler,但我的应用程序无法启动并显示以下消息:

Caused by: java.lang.IllegalArgumentException: Property 'messageWriters' is required
at org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler.afterPropertiesSet(AbstractErrorWebExceptionHandler.java:214) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]

我的处理程序(Kotlin 代码):

@Component
@Order(-2)
class SampleErrorWebExceptionHandler(
    errorAttributes: ErrorAttributes?,
    resourceProperties: ResourceProperties?,
    errorProperties: ErrorProperties?,
    applicationContext: ApplicationContext?
) : DefaultErrorWebExceptionHandler(errorAttributes, resourceProperties, errorProperties, applicationContext) {

    override fun logError(request: ServerRequest, errorStatus: HttpStatus) {
        // do something
    }
}

可能是什么原因?

您需要在该实例上设置 messageWriters,因为此处需要它们。您可能应该将其创建为 @Bean,就像 Spring Boot is doing in the dedicated auto-configuration.

请尝试在你的构造函数中添加ServerCodecConfigurer的依赖

GlobalErrorWebExceptionHandler(
  ErrorAttributes errorAttributes,
  ResourceProperties resourceProperties,
  ApplicationContext applicationContext,
  ServerCodecConfigurer configurer
) {
    super(errorAttributes, resourceProperties, applicationContext);
    this.setMessageWriters(configurer.getWriters());
}

我也是这样做的,在查看了 springs 实现之后,我只是将组件添加到构造函数中。

@Component
@Order(-2)
class GlobalErrorWebExceptionHandler(
        errorAttributes: ErrorAttributes,
        resourceProperties: ResourceProperties,
        applicationContext: ApplicationContext,
        viewResolvers: ObjectProvider<ViewResolver>,
        serverCodecConfigurer: ServerCodecConfigurer
) : AbstractErrorWebExceptionHandler(
        errorAttributes,
        resourceProperties,
        applicationContext
) {
    private val logger = LogFactory.getLog(GlobalErrorWebExceptionHandler::class.java)!!

    init {
        setViewResolvers(viewResolvers.orderedStream().collect(Collectors.toList()))
        setMessageWriters(serverCodecConfigurer.writers)
        setMessageReaders(serverCodecConfigurer.readers)
    }

    override fun getRoutingFunction(errorAttributes: ErrorAttributes) = RouterFunctions.route(RequestPredicates.all(), HandlerFunction { request ->
        val ex = getError(request)
        logger.error(ex.message)

        ServerResponse.ok().build()
    })
}

如果上面的解决方案不是很清楚请参考下面:

@Component
@Order(-2)
public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler {

    private final ObjectProvider<ViewResolver> viewResolvers;
    private final ServerCodecConfigurer serverCodecConfigurer;


    public GlobalErrorWebExceptionHandler(ObjectProvider<ViewResolver> viewResolvers, ServerCodecConfigurer serverCodecConfigurer,
                                          ErrorAttributes errorAttributes, WebProperties.Resources resources, ApplicationContext applicationContext) {
        super(errorAttributes, resources, applicationContext);
        this.viewResolvers = viewResolvers;
        this.serverCodecConfigurer = serverCodecConfigurer;
        super.setMessageWriters(serverCodecConfigurer.getWriters());
        super.setMessageReaders(serverCodecConfigurer.getReaders());
        super.setViewResolvers(viewResolvers.orderedStream().collect(Collectors.toList()));
    }

    @Override
    protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
        return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
    }

    private Mono<ServerResponse> renderErrorResponse(ServerRequest serverRequest) {
        Map<String, Object> errorPropertiesMap = getErrorAttributes(serverRequest,
                ErrorAttributeOptions.defaults());

        return ServerResponse.status(HttpStatus.BAD_REQUEST)
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(errorPropertiesMap));
    }
}