欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

springbootwebflux/gateway异常处理

时间:2023-07-10
相关类

WebExceptionHandler

public interface WebExceptionHandler {Mono handle(ServerWebExchange exchange, Throwable ex);}

ErrorWebExceptionHandler

@FunctionalInterfacepublic interface ErrorWebExceptionHandler extends WebExceptionHandler {}

自定义全局异常处理

GlobalExceptionHandler

@Component@Slf4jpublic class GlobalExceptionHandler implements ErrorWebExceptionHandler {// 打印日志时使用 private static final String ERROR_STACKTRACE = "Stacktrace:"; // 异常返回的状态码 private static final String KEY_STATUS = "httpStatus"; // 异常返回信息主体 private static final String KEY_BODY = "body"; private Tracer tracer; public void setTracer(Tracer tracer) { this.tracer = tracer; } private final ObjectMapper objectMapper = new ObjectMapper(); private List> messageReaders = Collections.emptyList(); private List> messageWriters = Collections.emptyList(); private List viewResolvers = Collections.emptyList(); private final ThreadLocal> exceptionHandlerResult = new ThreadLocal<>(); public void setMessageReaders(List> messageReaders) { Assert.notNull(messageReaders, "'messageReaders' must not be null"); this.messageReaders = messageReaders; } public void setViewResolvers(List viewResolvers) { this.viewResolvers = viewResolvers; } public void setMessageWriters(List> messageWriters) { Assert.notNull(messageWriters, "'messageWriters' must not be null"); this.messageWriters = messageWriters; } @SneakyThrows @Override public Mono handle(ServerWebExchange serverWebExchange, Throwable throwable) { // 按照异常类型进行处理 Map result = new HashMap<>(2, 1); ServerHttpRequest request = serverWebExchange.getRequest(); // 异常处理 此处添加自定义异常处理 if (throwable instanceof ResponseStatusException) { ResponseStatusException exception = (ResponseStatusException) throwable; CustomExceptionResponse customExceptionResponse = new CustomExceptionResponse(String.valueOf(exception.getRawStatusCode()), exception.getReason(),request.getURI().getPath()); log.debug("ResponseStatusException occurred, url: {}", request.getURI().getPath()); log.debug(ERROR_STACKTRACE, exception); String body = objectMapper.writevalueAsString(customExceptionResponse); result.put(KEY_STATUS, exception.getStatus()); result.put(KEY_BODY, body); } else { log.error("Exception occurred, url: {}", request.getURI().getPath()); log.error(ERROR_STACKTRACE, throwable); CustomExceptionResponse customExceptionResponse = new CustomExceptionResponse(null,throwable.getMessage(), request.getURI().getPath()); String body = objectMapper.writevalueAsString(customExceptionResponse); result.put(KEY_STATUS, HttpStatus.INTERNAL_SERVER_ERROR); result.put(KEY_BODY, body); } //参考AbstractErrorWebExceptionHandler if (serverWebExchange.getResponse().isCommitted()) { return Mono.error(throwable); } exceptionHandlerResult.set(result); ServerRequest newRequest = ServerRequest.create(serverWebExchange, this.messageReaders); Span span = tracer.currentSpan(); if (span != null) { span.tag("exception", throwable.toString()); span.flush(); } return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse).route(newRequest) .switchIfEmpty(Mono.error(throwable)) .flatMap((handler) -> handler.handle(newRequest)) .flatMap((response) -> write(serverWebExchange, response)); } protected Mono renderErrorResponse(ServerRequest request) { Map result = exceptionHandlerResult.get(); return ServerResponse.status((HttpStatus) result.get(KEY_STATUS)) .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters.fromValue(result.get(KEY_BODY))); } private Mono<? extends Void> write(ServerWebExchange exchange, ServerResponse response) { exchange.getResponse().getHeaders() .setContentType(response.headers().getContentType()); return response.writeTo(exchange, new ResponseContext()); } private class ResponseContext implements ServerResponse.Context { @Override public List> messageWriters() { return GlobalExceptionHandler.this.messageWriters; } @Override public List viewResolvers() { return GlobalExceptionHandler.this.viewResolvers; } }}

CustomExceptionResponse :

@Getter@Setter@AllArgsConstructor@NoArgsConstructorpublic class CustomExceptionResponse { private String errorCode; private String message; private String path;}

ExceptionConfig:

@Configurationpublic class ExceptionConfig { @Primary @Bean @Order(Ordered.HIGHEST_PRECEDENCE) public ErrorWebExceptionHandler errorWebExceptionHandler(ObjectProvider> viewResolversProvider, ServerCodecConfigurer serverCodecConfigurer, Tracer tracer) { GlobalExceptionHandler globalExceptionHandler = new GlobalExceptionHandler(); globalExceptionHandler.setTracer(tracer); globalExceptionHandler.setViewResolvers(viewResolversProvider.getIfAvailable(Collections::emptyList)); globalExceptionHandler.setMessageWriters(serverCodecConfigurer.getWriters()); globalExceptionHandler.setMessageReaders(serverCodecConfigurer.getReaders()); return globalExceptionHandler; }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。