Skip to content

Commit 6df8be8

Browse files
committed
Exclude query from URI in WebClient checkpoints
Closes gh-31992
1 parent 4a599d0 commit 6df8be8

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -212,7 +212,7 @@ public ClientResponse build() {
212212

213213
return new DefaultClientResponse(httpResponse, this.strategies,
214214
this.originalResponse != null ? this.originalResponse.logPrefix() : "",
215-
this.request.getMethod() + " " + this.request.getURI(),
215+
WebClientUtils.getRequestDescription(this.request.getMethod(), this.request.getURI()),
216216
() -> this.request);
217217
}
218218

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.web.reactive.function.client;
1818

1919
import java.net.URI;
20-
import java.net.URISyntaxException;
2120
import java.nio.charset.Charset;
2221
import java.time.ZonedDateTime;
2322
import java.util.ArrayList;
@@ -54,7 +53,6 @@
5453
import org.springframework.util.CollectionUtils;
5554
import org.springframework.util.LinkedMultiValueMap;
5655
import org.springframework.util.MultiValueMap;
57-
import org.springframework.util.StringUtils;
5856
import org.springframework.web.reactive.function.BodyExtractor;
5957
import org.springframework.web.reactive.function.BodyInserter;
6058
import org.springframework.web.reactive.function.BodyInserters;
@@ -457,7 +455,9 @@ public Mono<ClientResponse> exchange() {
457455
observationContext.setRequest(request);
458456
Mono<ClientResponse> responseMono = filterFunction.apply(exchangeFunction)
459457
.exchange(request)
460-
.checkpoint("Request to " + this.httpMethod.name() + " " + this.uri + " [DefaultWebClient]")
458+
.checkpoint("Request to " +
459+
WebClientUtils.getRequestDescription(request.method(), request.url()) +
460+
" [DefaultWebClient]")
461461
.switchIfEmpty(NO_HTTP_CLIENT_RESPONSE_ERROR);
462462
if (this.contextModifier != null) {
463463
responseMono = responseMono.contextWrite(this.contextModifier);
@@ -693,24 +693,13 @@ private <T> Mono<T> applyStatusHandlers(ClientResponse response) {
693693
}
694694
Mono<T> result = exMono.flatMap(Mono::error);
695695
return result.checkpoint(statusCode + " from " +
696-
this.httpMethod + " " + getUriToLog(this.uri) + " [DefaultWebClient]");
696+
WebClientUtils.getRequestDescription(this.httpMethod, this.uri) +
697+
" [DefaultWebClient]");
697698
}
698699
}
699700
return null;
700701
}
701702

702-
private static URI getUriToLog(URI uri) {
703-
if (StringUtils.hasText(uri.getQuery())) {
704-
try {
705-
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
706-
}
707-
catch (URISyntaxException ex) {
708-
// ignore
709-
}
710-
}
711-
return uri;
712-
}
713-
714703

715704
private static class StatusHandler {
716705

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -97,8 +97,8 @@ public WebClientResponseException(
9797
}
9898

9999
private static String initMessage(HttpStatusCode status, String reasonPhrase, @Nullable HttpRequest request) {
100-
return status.value() + " " + reasonPhrase +
101-
(request != null ? " from " + request.getMethod() + " " + request.getURI() : "");
100+
return status.value() + " " + reasonPhrase + (request != null ?
101+
" from " + WebClientUtils.getRequestDescription(request.getMethod(), request.getURI()) : "");
102102
}
103103

104104
/**

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientUtils.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.web.reactive.function.client;
1818

19+
import java.net.URI;
20+
import java.net.URISyntaxException;
1921
import java.util.List;
2022
import java.util.function.Predicate;
2123

@@ -24,7 +26,9 @@
2426
import reactor.core.publisher.Mono;
2527

2628
import org.springframework.core.codec.CodecException;
29+
import org.springframework.http.HttpMethod;
2730
import org.springframework.http.ResponseEntity;
31+
import org.springframework.util.StringUtils;
2832

2933
/**
3034
* Internal methods shared between {@link DefaultWebClient} and
@@ -64,4 +68,20 @@ public static <T> Mono<ResponseEntity<List<T>>> mapToEntityList(ClientResponse r
6468
new ResponseEntity<>(list, response.headers().asHttpHeaders(), response.statusCode()));
6569
}
6670

71+
/**
72+
* Return a String representation of the request details for logging purposes.
73+
* @since 6.0.16
74+
*/
75+
public static String getRequestDescription(HttpMethod httpMethod, URI uri) {
76+
if (StringUtils.hasText(uri.getQuery())) {
77+
try {
78+
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
79+
}
80+
catch (URISyntaxException ex) {
81+
// ignore
82+
}
83+
}
84+
return httpMethod.name() + " " + uri;
85+
}
86+
6787
}

0 commit comments

Comments
 (0)