Java Spring中的cutomizing ErrorAttributeOptions问题
在我的处理函数中,我有这个方法Java Spring中的cutomizing ErrorAttributeOptions问题,java,spring,spring-reactive,Java,Spring,Spring Reactive,在我的处理函数中,我有这个方法 public Mono<ServerResponse> itemsEx(ServerRequest serverRequest) { throw new RuntimeException("RuntimeException Occured"); } 问题是,我的终端仍然有一个堆栈跟踪l。。。但我只在message中明确定义了ErrorAttributeOptions。 我目前的Spring Boot版本是2.3.1。发布版
public Mono<ServerResponse> itemsEx(ServerRequest serverRequest) {
throw new RuntimeException("RuntimeException Occured");
}
问题是,我的终端仍然有一个堆栈跟踪l。。。但我只在message中明确定义了ErrorAttributeOptions。
我目前的Spring Boot版本是2.3.1。发布版。在以前的版本(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")));
}
}
package com.learnreactivespring.exception;
导入lombok.extern.slf4j.slf4j;
导入org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders;
导入org.springframework.boot.autoconfigure.web.ResourceProperties;
导入org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
导入org.springframework.boot.web.reactive.error.ErrorAttributes;
导入org.springframework.context.ApplicationContext;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.MediaType;
导入org.springframework.http.codec.servercodeconfigurer;
导入org.springframework.stereotype.Component;
导入org.springframework.web.reactive.function.BodyInserters;
导入org.springframework.web.reactive.function.server.*;
导入reactor.core.publisher.Mono;
导入java.util.Map;
@组成部分
@Slf4j
公共类函数RorWebExceptionHandler扩展了AbstractErrorWebExceptionHandler{
公共函数RorWebExceptionHandler(ErrorAttributes ErrorAttributes,
应用上下文应用上下文,
服务器编解码器配置器(服务器编解码器配置器){
超级(errorAttributes,new ResourceProperties(),applicationContext);
super.setMessageWriters(ServerCodeConfigurer.getWriters());
super.setMessageReaders(ServerCodeConfigurer.getReaders());
}
@凌驾
受保护的路由函数getRoutingFunction(ErrorAttributes ErrorAttributes){
返回RouterFunctions.route(RequestPredicates.all(),this::renderErrorResponse);
}
专用Mono renderErrorResponse(服务器请求服务器请求){
映射errorAttributesMap=getErrorAttributes(serverRequest,false);
log.info(“errorAttributesMap:+errorAttributesMap”);
返回ServerResponse.status(HttpStatus.INTERNAL\u SERVER\u错误)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromObject(errorAttributesMap.get(“消息”));
}
}
现在,在Spring Boot的颠簸版本之后,出现了这个问题。您应该使用
ErrorAttributeOptions.defaults()
。但并非默认情况下消息总是空的(并且stacktrace不存在)
因此,您可能希望使用以下内容:
ErrorAttributeOptions options = ErrorAttributeOptions
.defaults()
.including(ErrorAttributeOptions.Include.MESSAGE)
;
如果要排除某些内容,请使用
.excluding(.)
。您应该使用ErrorAttributeOptions.defaults()
。但并非默认情况下消息总是空的(并且stacktrace不存在)
因此,您可能希望使用以下内容:
ErrorAttributeOptions options = ErrorAttributeOptions
.defaults()
.including(ErrorAttributeOptions.Include.MESSAGE)
;
如果要排除某些内容,请使用.excluding(.)