【问题标题】:Problem with cutomizing ErrorAttributeOptions in Spring Reactive在 Spring Reactive 中自定义 ErrorAttributeOptions 的问题
【发布时间】:2020-08-06 17:48:10
【问题描述】:

在我的处理函数中我有这个方法

public Mono<ServerResponse> itemsEx(ServerRequest serverRequest) {
    throw new RuntimeException("RuntimeException Occured");
}

现在,我想处理这个异常,所以我重写 AbstractErrorWebExceptionHandler 并创建这个类。

package com.learnreactivespring.learnreactivespring.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.*;
import reactor.core.publisher.Mono;

import java.util.Map;

@Component
@Slf4j
public class FunctionalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler {


public FunctionalErrorWebExceptionHandler(ErrorAttributes errorAttributes,
                                          ApplicationContext applicationContext,
                                          ServerCodecConfigurer serverCodecConfigurer) {
    super(errorAttributes, new ResourceProperties(), applicationContext);
    super.setMessageWriters(serverCodecConfigurer.getWriters());
    super.setMessageReaders(serverCodecConfigurer.getReaders());
}

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

}

private  Mono<ServerResponse> renderErrorResponse(ServerRequest serverRequest) {

    ErrorAttributeOptions errorAttributes = ErrorAttributeOptions.of(ErrorAttributeOptions.Include.MESSAGE);

    Map<String, Object> errorAttributesMap = getErrorAttributes(serverRequest, errorAttributes);
    log.info("errorAttributesMap : " + errorAttributesMap);

    return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR)
            .contentType(MediaType.APPLICATION_JSON)
            .body(BodyInserters.fromValue(errorAttributesMap.get("message")));

}

问题是,我的终端中仍然有一个 Stacktrace...但我仅使用 message 明确定义了 ErrorAttributeOptions。 我当前的 Spring Boot 版本是 2.3.1.RELEASE。在以前的版本(2.1.1)我没有这个问题,因为我习惯了这个

package com.learnreactivespring.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.*;
import reactor.core.publisher.Mono;

import java.util.Map;

@Component
@Slf4j
public class FunctionalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler {


    public FunctionalErrorWebExceptionHandler(ErrorAttributes errorAttributes,
                                              ApplicationContext applicationContext,
                                              ServerCodecConfigurer serverCodecConfigurer) {
        super(errorAttributes, new ResourceProperties(), applicationContext);
        super.setMessageWriters(serverCodecConfigurer.getWriters());
        super.setMessageReaders(serverCodecConfigurer.getReaders());
    }

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

    }

    private  Mono<ServerResponse> renderErrorResponse(ServerRequest serverRequest) {

        Map<String, Object> errorAttributesMap = getErrorAttributes(serverRequest, false);
        log.info("errorAttributesMap : " + errorAttributesMap);

        return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromObject(errorAttributesMap.get("message")));

    }


}

现在,在 Spring Boot 的碰撞版本之后发生了这个问题。

【问题讨论】:

    标签: java spring spring-reactive


    【解决方案1】:

    您应该使用ErrorAttributeOptions.defaults()。但请注意,默认情况下消息始终为空(并且堆栈跟踪不存在)。

    所以你可能想使用这样的东西:

    ErrorAttributeOptions options = ErrorAttributeOptions
        .defaults()
        .including(ErrorAttributeOptions.Include.MESSAGE)
    ;
    

    如果您想排除某些内容,请使用.excluding(.)

    【讨论】:

      猜你喜欢
      • 2020-12-29
      • 2019-04-11
      • 2013-09-25
      • 2020-07-05
      • 2020-04-20
      • 2020-12-20
      • 1970-01-01
      • 2012-09-13
      • 2015-10-19
      相关资源
      最近更新 更多