diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java index 5ba831ebd8ae..e089b1f3ccb2 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -212,7 +212,7 @@ public ClientResponse build() { return new DefaultClientResponse(httpResponse, this.strategies, this.originalResponse != null ? this.originalResponse.logPrefix() : "", - this.request.getMethod() + " " + this.request.getURI(), + WebClientUtils.getRequestDescription(this.request.getMethod(), this.request.getURI()), () -> this.request); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java index 91a5b57b5774..31deb88261da 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ package org.springframework.web.reactive.function.client; import java.net.URI; -import java.net.URISyntaxException; import java.nio.charset.Charset; import java.time.ZonedDateTime; import java.util.ArrayList; @@ -54,7 +53,6 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import org.springframework.util.StringUtils; import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.reactive.function.BodyInserter; import org.springframework.web.reactive.function.BodyInserters; @@ -457,7 +455,9 @@ public Mono exchange() { observationContext.setRequest(request); Mono responseMono = filterFunction.apply(exchangeFunction) .exchange(request) - .checkpoint("Request to " + this.httpMethod.name() + " " + this.uri + " [DefaultWebClient]") + .checkpoint("Request to " + + WebClientUtils.getRequestDescription(request.method(), request.url()) + + " [DefaultWebClient]") .switchIfEmpty(NO_HTTP_CLIENT_RESPONSE_ERROR); if (this.contextModifier != null) { responseMono = responseMono.contextWrite(this.contextModifier); @@ -693,24 +693,13 @@ private Mono applyStatusHandlers(ClientResponse response) { } Mono result = exMono.flatMap(Mono::error); return result.checkpoint(statusCode + " from " + - this.httpMethod + " " + getUriToLog(this.uri) + " [DefaultWebClient]"); + WebClientUtils.getRequestDescription(this.httpMethod, this.uri) + + " [DefaultWebClient]"); } } return null; } - private static URI getUriToLog(URI uri) { - if (StringUtils.hasText(uri.getQuery())) { - try { - uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null); - } - catch (URISyntaxException ex) { - // ignore - } - } - return uri; - } - private static class StatusHandler { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java index 057cc0b09562..47e04f9afe8c 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -97,8 +97,8 @@ public WebClientResponseException( } private static String initMessage(HttpStatusCode status, String reasonPhrase, @Nullable HttpRequest request) { - return status.value() + " " + reasonPhrase + - (request != null ? " from " + request.getMethod() + " " + request.getURI() : ""); + return status.value() + " " + reasonPhrase + (request != null ? + " from " + WebClientUtils.getRequestDescription(request.getMethod(), request.getURI()) : ""); } /** diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientUtils.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientUtils.java index dae54ccb1382..62cfd503bda9 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientUtils.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.web.reactive.function.client; +import java.net.URI; +import java.net.URISyntaxException; import java.util.List; import java.util.function.Predicate; @@ -24,7 +26,9 @@ import reactor.core.publisher.Mono; import org.springframework.core.codec.CodecException; +import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; +import org.springframework.util.StringUtils; /** * Internal methods shared between {@link DefaultWebClient} and @@ -64,4 +68,20 @@ public static Mono>> mapToEntityList(ClientResponse r new ResponseEntity<>(list, response.headers().asHttpHeaders(), response.statusCode())); } + /** + * Return a String representation of the request details for logging purposes. + * @since 6.0.16 + */ + public static String getRequestDescription(HttpMethod httpMethod, URI uri) { + if (StringUtils.hasText(uri.getQuery())) { + try { + uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null); + } + catch (URISyntaxException ex) { + // ignore + } + } + return httpMethod.name() + " " + uri; + } + }